/
wlanctl.c
348 lines (297 loc) · 9.08 KB
/
wlanctl.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
/* $NetBSD: wlanctl.c,v 1.10 2007/12/22 00:58:15 dyoung Exp $ */
/*-
* Copyright (c) 2005 David Young. All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. The name of David Young may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
* Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/inttypes.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_media.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_sysctl.h>
struct flagname {
u_int32_t fn_flag;
const char *fn_name;
};
struct cmdflags {
int cf_v; /* verbose */
int cf_a; /* all 802.11 interfaces */
int cf_p; /* public (i.e. non-private) dests */
};
static void print_flags(u_int32_t, const struct flagname *, u_int);
static int dump_nodes(const char *, int, struct cmdflags *);
static const char *ether_string(u_int8_t *);
static void parse_args(int *, char ***, struct cmdflags *);
static void print_capinfo(u_int16_t);
static void print_channel(u_int16_t, u_int16_t, u_int16_t);
static void print_node_flags(u_int32_t);
static void print_rateset(struct ieee80211_rateset *, int);
static void usage(void);
static void
print_rateset(struct ieee80211_rateset *rs, int txrate)
{
int i, rate;
const char *fmt, *basic;
printf("\trates");
for (i = 0; i < rs->rs_nrates; i++) {
if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) != 0)
basic = "*";
else
basic = "";
if (i == txrate)
fmt = " [%s%d.%d]";
else
fmt = " %s%d.%d";
rate = 5 * (rs->rs_rates[i] & IEEE80211_RATE_VAL);
printf(fmt, basic, rate / 10, rate % 10);
}
printf("\n");
}
static void
print_flags(u_int32_t flags, const struct flagname *flagnames, u_int nname)
{
int i;
const char *delim;
delim = "<";
for (i = 0; i < nname; i++) {
if ((flags & flagnames[i].fn_flag) != 0) {
printf("%s%s", delim, flagnames[i].fn_name);
delim = ",";
}
}
printf("%s\n", (delim[0] == '<') ? "" : ">");
}
static void
print_node_flags(u_int32_t flags)
{
const static struct flagname nodeflags[] = {
{IEEE80211_NODE_SYSCTL_F_BSS, "bss"}
, {IEEE80211_NODE_SYSCTL_F_STA, "sta"}
, {IEEE80211_NODE_SYSCTL_F_SCAN, "scan"}
};
printf("\tnode flags %04x", flags);
print_flags(flags, nodeflags, __arraycount(nodeflags));
}
static void
print_capinfo(u_int16_t capinfo)
{
const static struct flagname capflags[] = {
{IEEE80211_CAPINFO_ESS, "ess"},
{IEEE80211_CAPINFO_IBSS, "ibss"},
{IEEE80211_CAPINFO_CF_POLLABLE, "cf pollable"},
{IEEE80211_CAPINFO_CF_POLLREQ, "request cf poll"},
{IEEE80211_CAPINFO_PRIVACY, "privacy"},
{IEEE80211_CAPINFO_SHORT_PREAMBLE, "short preamble"},
{IEEE80211_CAPINFO_PBCC, "pbcc"},
{IEEE80211_CAPINFO_CHNL_AGILITY, "channel agility"},
{IEEE80211_CAPINFO_SHORT_SLOTTIME, "short slot-time"},
{IEEE80211_CAPINFO_RSN, "rsn"},
{IEEE80211_CAPINFO_DSSSOFDM, "dsss-ofdm"}
};
printf("\tcapabilities %04x", capinfo);
print_flags(capinfo, capflags, __arraycount(capflags));
}
static const char *
ether_string(u_int8_t *addr)
{
struct ether_addr ea;
(void)memcpy(ea.ether_addr_octet, addr, sizeof(ea.ether_addr_octet));
return ether_ntoa(&ea);
}
static void
print_channel(u_int16_t chanidx, u_int16_t freq, u_int16_t flags)
{
const static struct flagname chanflags[] = {
{IEEE80211_CHAN_TURBO, "turbo"},
{IEEE80211_CHAN_CCK, "cck"},
{IEEE80211_CHAN_OFDM, "ofdm"},
{IEEE80211_CHAN_2GHZ, "2.4GHz"},
{IEEE80211_CHAN_5GHZ, "5GHz"},
{IEEE80211_CHAN_PASSIVE, "passive scan"},
{IEEE80211_CHAN_DYN, "dynamic cck-ofdm"},
{IEEE80211_CHAN_GFSK, "gfsk"}
};
printf("\tchan %d freq %dMHz flags %04x", chanidx, freq, flags);
print_flags(flags, chanflags, __arraycount(chanflags));
}
/*
*
* ifname: dump nodes belonging to the given interface, or belonging
* to all interfaces if NULL
* hdr_type: header type: IEEE80211_SYSCTL_T_NODE -> generic node,
* IEEE80211_SYSCTL_T_RSSADAPT -> rssadapt(9) info,
* IEEE80211_SYSCTL_T_DRVSPEC -> driver specific.
* cf: command flags, cf_v != 0 -> verbose
*/
static int
dump_nodes(const char *ifname_arg, int hdr_type, struct cmdflags *cf)
{
#if 0
/*39*/ u_int8_t ns_erp; /* 11g only */
/*40*/ u_int32_t ns_rstamp; /* recv timestamp */
/*64*/ u_int16_t ns_fhdwell; /* FH only */
/*66*/ u_int8_t ns_fhindex; /* FH only */
/*68*/
#endif
u_int i, ifindex;
size_t namelen, nodes_len, totallen;
int name[12];
int *vname;
char ifname[IFNAMSIZ];
struct ieee80211_node_sysctl *pns, *ns;
u_int64_t ts;
namelen = __arraycount(name);
if (sysctlnametomib("net.link.ieee80211.nodes", &name[0],
&namelen) != 0) {
warn("sysctlnametomib");
return -1;
}
if (ifname_arg == NULL)
ifindex = 0;
else if ((ifindex = if_nametoindex(ifname_arg)) == 0) {
warn("if_nametoindex");
return -1;
}
totallen = namelen + IEEE80211_SYSCTL_NODENAMELEN;
if (totallen >= __arraycount(name)) {
warnx("Internal error finding sysctl mib");
return -1;
}
vname = &name[namelen];
vname[IEEE80211_SYSCTL_NODENAME_IF] = ifindex;
vname[IEEE80211_SYSCTL_NODENAME_OP] = IEEE80211_SYSCTL_OP_ALL;
vname[IEEE80211_SYSCTL_NODENAME_ARG] = 0;
vname[IEEE80211_SYSCTL_NODENAME_TYPE] = hdr_type;
vname[IEEE80211_SYSCTL_NODENAME_ELTSIZE] = sizeof(*ns);
vname[IEEE80211_SYSCTL_NODENAME_ELTCOUNT] = INT_MAX;
/* how many? */
if (sysctl(name, totallen, NULL, &nodes_len, NULL, 0) != 0) {
warn("sysctl(count)");
return -1;
}
ns = malloc(nodes_len);
if (ns == NULL) {
warn("malloc");
return -1;
}
vname[IEEE80211_SYSCTL_NODENAME_ELTCOUNT] = nodes_len / sizeof(ns[0]);
/* Get them. */
if (sysctl(name, totallen, ns, &nodes_len, NULL, 0) != 0) {
warn("sysctl(get)");
return -1;
}
for (i = 0; i < nodes_len / sizeof(ns[0]); i++) {
pns = &ns[i];
if (if_indextoname(pns->ns_ifindex, ifname) == NULL) {
warn("if_indextoname");
return -1;
}
if (cf->cf_p && (pns->ns_capinfo & IEEE80211_CAPINFO_PRIVACY))
continue;
printf("%s: mac %s ", ifname, ether_string(pns->ns_macaddr));
printf("bss %s\n", ether_string(pns->ns_bssid));
print_node_flags(pns->ns_flags);
/* TBD deal with binary ESSID */
printf("\tess <%.*s>\n", pns->ns_esslen, pns->ns_essid);
print_channel(pns->ns_chanidx, pns->ns_freq, pns->ns_chanflags);
print_capinfo(pns->ns_capinfo);
assert(sizeof(ts) == sizeof(pns->ns_tstamp));
memcpy(&ts, &pns->ns_tstamp[0], sizeof(ts));
printf("\tbeacon-interval %d TU tsft %" PRIu64 " us\n",
pns->ns_intval, (u_int64_t)le64toh(ts));
print_rateset(&pns->ns_rates, pns->ns_txrate);
printf("\tassoc-id %d assoc-failed %d inactivity %ds\n",
pns->ns_associd, pns->ns_fails, pns->ns_inact);
printf("\trssi %d txseq %d rxseq %d\n",
pns->ns_rssi, pns->ns_txseq, pns->ns_rxseq);
}
return 0;
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [ -p ] [ -v ] -a\n"
"\t[ -v ] interface [ interface ... ]\n", getprogname());
exit(EXIT_FAILURE);
}
static void
parse_args(int *argcp, char ***argvp, struct cmdflags *cf)
{
int ch;
(void)memset(cf, 0, sizeof(*cf));
while ((ch = getopt(*argcp, *argvp, "apv")) != -1) {
switch (ch) {
case 'a':
cf->cf_a = 1;
break;
case 'p':
cf->cf_p = 1;
break;
case 'v':
cf->cf_v = 1;
break;
default:
warnx("unknown option -%c", ch);
usage();
}
}
*argcp -= optind;
*argvp += optind;
}
#define LOGICAL_XOR(x, y) (!(x) != !(y))
int
main(int argc, char **argv)
{
int i;
struct cmdflags cf;
parse_args(&argc, &argv, &cf);
if (!LOGICAL_XOR(argc > 0, cf.cf_a))
usage();
if (cf.cf_a) {
if (dump_nodes(NULL, IEEE80211_SYSCTL_T_NODE, &cf) != 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
for (i = 0; i < argc; i++) {
if (dump_nodes(argv[i], IEEE80211_SYSCTL_T_NODE, &cf) != 0)
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}