wlanctl.c revision 1.9 1 /* $NetBSD: wlanctl.c,v 1.9 2007/08/29 02:27:55 dogcow Exp $ */
2 /*-
3 * Copyright (c) 2005 David Young. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
7 * conditions are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. The name of David Young may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
22 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29 * OF SUCH DAMAGE.
30 */
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <assert.h>
37
38 #include <sys/param.h>
39 #include <sys/sysctl.h>
40 #include <sys/inttypes.h>
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44
45 #include <net/if.h>
46 #include <net/if_media.h>
47 #include <netinet/in.h>
48 #include <netinet/if_ether.h>
49
50 #include <net80211/ieee80211.h>
51 #include <net80211/ieee80211_node.h>
52 #include <net80211/ieee80211_sysctl.h>
53
54 #define NELTS(x) (sizeof(x)/sizeof((x)[0]))
55
56 struct flagname {
57 u_int32_t fn_flag;
58 const char *fn_name;
59 };
60
61 struct cmdflags {
62 int cf_v; /* verbose */
63 int cf_a; /* all 802.11 interfaces */
64 int cf_p; /* public (i.e. non-private) dests */
65 };
66
67 static void print_flags(u_int32_t, const struct flagname *, u_int);
68 static int dump_nodes(const char *, int, struct cmdflags *);
69 static const char *ether_string(u_int8_t *);
70 static void parse_args(int *, char ***, struct cmdflags *);
71 static void print_capinfo(u_int16_t);
72 static void print_channel(u_int16_t, u_int16_t, u_int16_t);
73 static void print_node_flags(u_int32_t);
74 static void print_rateset(struct ieee80211_rateset *, int);
75 static void usage(void);
76
77 static void
78 print_rateset(struct ieee80211_rateset *rs, int txrate)
79 {
80 int i, rate;
81 const char *fmt, *basic;
82
83 printf("\trates");
84
85 for (i = 0; i < rs->rs_nrates; i++) {
86
87 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) != 0)
88 basic = "*";
89 else
90 basic = "";
91 if (i == txrate)
92 fmt = " [%s%d.%d]";
93 else
94 fmt = " %s%d.%d";
95 rate = 5 * (rs->rs_rates[i] & IEEE80211_RATE_VAL);
96 printf(fmt, basic, rate / 10, rate % 10);
97 }
98 printf("\n");
99 }
100
101 static void
102 print_flags(u_int32_t flags, const struct flagname *flagnames, u_int nname)
103 {
104 int i;
105 const char *delim;
106 delim = "<";
107
108 for (i = 0; i < nname; i++) {
109 if ((flags & flagnames[i].fn_flag) != 0) {
110 printf("%s%s", delim, flagnames[i].fn_name);
111 delim = ",";
112 }
113 }
114
115 printf("%s\n", (delim[0] == '<') ? "" : ">");
116 }
117
118 static void
119 print_node_flags(u_int32_t flags)
120 {
121 const static struct flagname nodeflags[] = {
122 {IEEE80211_NODE_SYSCTL_F_BSS, "bss"}
123 , {IEEE80211_NODE_SYSCTL_F_STA, "sta"}
124 , {IEEE80211_NODE_SYSCTL_F_SCAN, "scan"}
125 };
126 printf("\tnode flags %04x", flags);
127
128 print_flags(flags, nodeflags, NELTS(nodeflags));
129 }
130
131 static void
132 print_capinfo(u_int16_t capinfo)
133 {
134 const static struct flagname capflags[] = {
135 {IEEE80211_CAPINFO_ESS, "ess"},
136 {IEEE80211_CAPINFO_IBSS, "ibss"},
137 {IEEE80211_CAPINFO_CF_POLLABLE, "cf pollable"},
138 {IEEE80211_CAPINFO_CF_POLLREQ, "request cf poll"},
139 {IEEE80211_CAPINFO_PRIVACY, "privacy"},
140 {IEEE80211_CAPINFO_SHORT_PREAMBLE, "short preamble"},
141 {IEEE80211_CAPINFO_PBCC, "pbcc"},
142 {IEEE80211_CAPINFO_CHNL_AGILITY, "channel agility"},
143 {IEEE80211_CAPINFO_SHORT_SLOTTIME, "short slot-time"},
144 {IEEE80211_CAPINFO_RSN, "rsn"},
145 {IEEE80211_CAPINFO_DSSSOFDM, "dsss-ofdm"}
146 };
147
148 printf("\tcapabilities %04x", capinfo);
149
150 print_flags(capinfo, capflags, NELTS(capflags));
151 }
152
153 static const char *
154 ether_string(u_int8_t *addr)
155 {
156 struct ether_addr ea;
157 (void)memcpy(ea.ether_addr_octet, addr, sizeof(ea.ether_addr_octet));
158 return ether_ntoa(&ea);
159 }
160
161 static void
162 print_channel(u_int16_t chanidx, u_int16_t freq, u_int16_t flags)
163 {
164 const static struct flagname chanflags[] = {
165 {IEEE80211_CHAN_TURBO, "turbo"},
166 {IEEE80211_CHAN_CCK, "cck"},
167 {IEEE80211_CHAN_OFDM, "ofdm"},
168 {IEEE80211_CHAN_2GHZ, "2.4GHz"},
169 {IEEE80211_CHAN_5GHZ, "5GHz"},
170 {IEEE80211_CHAN_PASSIVE, "passive scan"},
171 {IEEE80211_CHAN_DYN, "dynamic cck-ofdm"},
172 {IEEE80211_CHAN_GFSK, "gfsk"}
173 };
174 printf("\tchan %d freq %dMHz flags %04x", chanidx, freq, flags);
175
176 print_flags(flags, chanflags, NELTS(chanflags));
177 }
178
179 /*
180 *
181 * ifname: dump nodes belonging to the given interface, or belonging
182 * to all interfaces if NULL
183 * hdr_type: header type: IEEE80211_SYSCTL_T_NODE -> generic node,
184 * IEEE80211_SYSCTL_T_RSSADAPT -> rssadapt(9) info,
185 * IEEE80211_SYSCTL_T_DRVSPEC -> driver specific.
186 * cf: command flags, cf_v != 0 -> verbose
187 */
188 static int
189 dump_nodes(const char *ifname_arg, int hdr_type, struct cmdflags *cf)
190 {
191 #if 0
192 /*39*/ u_int8_t ns_erp; /* 11g only */
193 /*40*/ u_int32_t ns_rstamp; /* recv timestamp */
194 /*64*/ u_int16_t ns_fhdwell; /* FH only */
195 /*66*/ u_int8_t ns_fhindex; /* FH only */
196 /*68*/
197 #endif
198 u_int i, ifindex;
199 size_t namelen, nodes_len, totallen;
200 int name[12];
201 int *vname;
202 char ifname[IFNAMSIZ];
203 struct ieee80211_node_sysctl *pns, *ns;
204 u_int64_t ts;
205
206 namelen = NELTS(name);
207
208 if (sysctlnametomib("net.link.ieee80211.nodes", &name[0],
209 &namelen) != 0) {
210 warn("sysctlnametomib");
211 return -1;
212 }
213
214 if (ifname_arg == NULL)
215 ifindex = 0;
216 else if ((ifindex = if_nametoindex(ifname_arg)) == 0) {
217 warn("if_nametoindex");
218 return -1;
219 }
220
221 totallen = namelen + IEEE80211_SYSCTL_NODENAMELEN;
222 if (totallen >= NELTS(name)) {
223 warnx("Internal error finding sysctl mib");
224 return -1;
225 }
226 vname = &name[namelen];
227
228 vname[IEEE80211_SYSCTL_NODENAME_IF] = ifindex;
229 vname[IEEE80211_SYSCTL_NODENAME_OP] = IEEE80211_SYSCTL_OP_ALL;
230 vname[IEEE80211_SYSCTL_NODENAME_ARG] = 0;
231 vname[IEEE80211_SYSCTL_NODENAME_TYPE] = hdr_type;
232 vname[IEEE80211_SYSCTL_NODENAME_ELTSIZE] = sizeof(*ns);
233 vname[IEEE80211_SYSCTL_NODENAME_ELTCOUNT] = INT_MAX;
234
235 /* how many? */
236 if (sysctl(name, totallen, NULL, &nodes_len, NULL, 0) != 0) {
237 warn("sysctl(count)");
238 return -1;
239 }
240
241 ns = malloc(nodes_len);
242
243 if (ns == NULL) {
244 warn("malloc");
245 return -1;
246 }
247
248 vname[IEEE80211_SYSCTL_NODENAME_ELTCOUNT] = nodes_len / sizeof(ns[0]);
249
250 /* Get them. */
251 if (sysctl(name, totallen, ns, &nodes_len, NULL, 0) != 0) {
252 warn("sysctl(get)");
253 return -1;
254 }
255
256 for (i = 0; i < nodes_len / sizeof(ns[0]); i++) {
257 pns = &ns[i];
258 if (if_indextoname(pns->ns_ifindex, ifname) == NULL) {
259 warn("if_indextoname");
260 return -1;
261 }
262 if (cf->cf_p && (pns->ns_capinfo & IEEE80211_CAPINFO_PRIVACY))
263 continue;
264 printf("%s: mac %s ", ifname, ether_string(pns->ns_macaddr));
265 printf("bss %s\n", ether_string(pns->ns_bssid));
266 print_node_flags(pns->ns_flags);
267
268 /* TBD deal with binary ESSID */
269 printf("\tess <%.*s>\n", pns->ns_esslen, pns->ns_essid);
270
271 print_channel(pns->ns_chanidx, pns->ns_freq, pns->ns_chanflags);
272
273 print_capinfo(pns->ns_capinfo);
274
275 assert(sizeof(ts) == sizeof(pns->ns_tstamp));
276 memcpy(&ts, &pns->ns_tstamp[0], sizeof(ts));
277 printf("\tbeacon-interval %d TU tsft %" PRIu64 " us\n",
278 pns->ns_intval, (u_int64_t)le64toh(ts));
279
280 print_rateset(&pns->ns_rates, pns->ns_txrate);
281
282 printf("\tassoc-id %d assoc-failed %d inactivity %ds\n",
283 pns->ns_associd, pns->ns_fails, pns->ns_inact);
284
285 printf("\trssi %d txseq %d rxseq %d\n",
286 pns->ns_rssi, pns->ns_txseq, pns->ns_rxseq);
287 }
288 return 0;
289 }
290
291 static void
292 usage(void)
293 {
294 fprintf(stderr, "usage: %s [ -p ] [ -v ] -a\n"
295 "\t[ -v ] interface [ interface ... ]\n", getprogname());
296 exit(EXIT_FAILURE);
297 }
298
299 static void
300 parse_args(int *argcp, char ***argvp, struct cmdflags *cf)
301 {
302 int ch;
303
304 (void)memset(cf, 0, sizeof(*cf));
305
306 while ((ch = getopt(*argcp, *argvp, "apv")) != -1) {
307 switch (ch) {
308 case 'a':
309 cf->cf_a = 1;
310 break;
311 case 'p':
312 cf->cf_p = 1;
313 break;
314 case 'v':
315 cf->cf_v = 1;
316 break;
317 default:
318 warnx("unknown option -%c", ch);
319 usage();
320 }
321 }
322
323 *argcp -= optind;
324 *argvp += optind;
325 }
326
327 #define LOGICAL_XOR(x, y) (!(x) != !(y))
328
329 int
330 main(int argc, char **argv)
331 {
332 int i;
333 struct cmdflags cf;
334
335 parse_args(&argc, &argv, &cf);
336
337 if (!LOGICAL_XOR(argc > 0, cf.cf_a))
338 usage();
339
340 if (cf.cf_a) {
341 if (dump_nodes(NULL, IEEE80211_SYSCTL_T_NODE, &cf) != 0)
342 return EXIT_FAILURE;
343 return EXIT_SUCCESS;
344 }
345 for (i = 0; i < argc; i++) {
346 if (dump_nodes(argv[i], IEEE80211_SYSCTL_T_NODE, &cf) != 0)
347 return EXIT_FAILURE;
348 }
349
350 return EXIT_SUCCESS;
351 }
352