xref: /netbsd-src/usr.sbin/wlanctl/wlanctl.c (revision 29f735df08dafc6ecec1adeaac83e13e33404a82)
1 /* $NetBSD: wlanctl.c,v 1.14 2014/04/22 15:55:16 christos 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_sysctl.h>
52 
53 struct flagname {
54 	u_int32_t fn_flag;
55 	const char *fn_name;
56 };
57 
58 struct cmdflags {
59 	int	cf_a;	/* all 802.11 interfaces */
60   	int     cf_p;   /* public (i.e. non-private) dests */
61 };
62 
63 static void		print_flags(u_int32_t, const struct flagname *, u_int);
64 static int		dump_nodes(const char *, int, struct cmdflags *);
65 static const char	*ether_string(u_int8_t *);
66 static void		parse_args(int *, char ***, struct cmdflags *);
67 static void		print_capinfo(u_int16_t);
68 static void		print_channel(u_int16_t, u_int16_t, u_int16_t);
69 static void		print_node_flags(u_int32_t);
70 static void		print_rateset(struct ieee80211_rateset *, int);
71 __dead static void	usage(void);
72 
73 static void
print_rateset(struct ieee80211_rateset * rs,int txrate)74 print_rateset(struct ieee80211_rateset *rs, int txrate)
75 {
76 	int i, rate;
77 	const char *basic;
78 
79 	printf("\trates");
80 
81 	for (i = 0; i < rs->rs_nrates; i++) {
82 
83 		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) != 0)
84 			basic = "*";
85 		else
86 			basic = "";
87 		rate = 5 * (rs->rs_rates[i] & IEEE80211_RATE_VAL);
88 		if (i == txrate)
89 			printf(" [%s%d.%d]", basic, rate / 10, rate % 10);
90 		else
91 			printf(" %s%d.%d", basic, rate / 10, rate % 10);
92 	}
93 	printf("\n");
94 }
95 
96 static void
print_flags(u_int32_t flags,const struct flagname * flagnames,u_int nname)97 print_flags(u_int32_t flags, const struct flagname *flagnames, u_int nname)
98 {
99 	u_int i;
100 	const char *delim;
101 	delim = "<";
102 
103 	for (i = 0; i < nname; i++) {
104 		if ((flags & flagnames[i].fn_flag) != 0) {
105 			printf("%s%s", delim, flagnames[i].fn_name);
106 			delim = ",";
107 		}
108 	}
109 
110 	printf("%s\n", (delim[0] == '<') ? "" : ">");
111 }
112 
113 static void
print_node_flags(u_int32_t flags)114 print_node_flags(u_int32_t flags)
115 {
116 	static const struct flagname nodeflags[] = {
117 		  {IEEE80211_NODE_SYSCTL_F_BSS, "bss"}
118 		, {IEEE80211_NODE_SYSCTL_F_STA, "sta"}
119 		, {IEEE80211_NODE_SYSCTL_F_SCAN, "scan"}
120 	};
121 	printf("\tnode flags %04x", flags);
122 
123 	print_flags(flags, nodeflags, __arraycount(nodeflags));
124 }
125 
126 static void
print_capinfo(u_int16_t capinfo)127 print_capinfo(u_int16_t capinfo)
128 {
129 	static const struct flagname capflags[] = {
130 		{IEEE80211_CAPINFO_ESS, "ess"},
131 		{IEEE80211_CAPINFO_IBSS, "ibss"},
132 		{IEEE80211_CAPINFO_CF_POLLABLE, "cf pollable"},
133 		{IEEE80211_CAPINFO_CF_POLLREQ, "request cf poll"},
134 		{IEEE80211_CAPINFO_PRIVACY, "privacy"},
135 		{IEEE80211_CAPINFO_SHORT_PREAMBLE, "short preamble"},
136 		{IEEE80211_CAPINFO_PBCC, "pbcc"},
137 		{IEEE80211_CAPINFO_CHNL_AGILITY, "channel agility"},
138 		{IEEE80211_CAPINFO_SHORT_SLOTTIME, "short slot-time"},
139 		{IEEE80211_CAPINFO_RSN, "rsn"},
140 		{IEEE80211_CAPINFO_DSSSOFDM, "dsss-ofdm"}
141 	};
142 
143 	printf("\tcapabilities %04x", capinfo);
144 
145 	print_flags(capinfo, capflags, __arraycount(capflags));
146 }
147 
148 static const char *
ether_string(u_int8_t * addr)149 ether_string(u_int8_t *addr)
150 {
151 	struct ether_addr ea;
152 	(void)memcpy(ea.ether_addr_octet, addr, sizeof(ea.ether_addr_octet));
153 	return ether_ntoa(&ea);
154 }
155 
156 static void
print_channel(u_int16_t chanidx,u_int16_t freq,u_int16_t flags)157 print_channel(u_int16_t chanidx, u_int16_t freq, u_int16_t flags)
158 {
159 	static const struct flagname chanflags[] = {
160 		{IEEE80211_CHAN_TURBO, "turbo"},
161 		{IEEE80211_CHAN_CCK, "cck"},
162 		{IEEE80211_CHAN_OFDM, "ofdm"},
163 		{IEEE80211_CHAN_2GHZ, "2.4GHz"},
164 		{IEEE80211_CHAN_5GHZ, "5GHz"},
165 		{IEEE80211_CHAN_PASSIVE, "passive scan"},
166 		{IEEE80211_CHAN_DYN, "dynamic cck-ofdm"},
167 		{IEEE80211_CHAN_GFSK, "gfsk"}
168 	};
169 	printf("\tchan %d freq %dMHz flags %04x", chanidx, freq, flags);
170 
171 	print_flags(flags, chanflags, __arraycount(chanflags));
172 }
173 
174 /*
175  *
176  * ifname:   dump nodes belonging to the given interface, or belonging
177  *           to all interfaces if NULL
178  * hdr_type: header type: IEEE80211_SYSCTL_T_NODE -> generic node,
179  *                        IEEE80211_SYSCTL_T_RSSADAPT -> rssadapt(9) info,
180  *                        IEEE80211_SYSCTL_T_DRVSPEC -> driver specific.
181  * cf:       command flags: cf_a != 0 -> all 802.11 interfaces
182  *                          cf_p != 0 -> public dests
183  */
184 static int
dump_nodes(const char * ifname_arg,int hdr_type,struct cmdflags * cf)185 dump_nodes(const char *ifname_arg, int hdr_type, struct cmdflags *cf)
186 {
187 #if 0
188 /*39*/	u_int8_t	ns_erp;		/* 11g only */
189 /*40*/	u_int32_t	ns_rstamp;	/* recv timestamp */
190 /*64*/	u_int16_t	ns_fhdwell;	/* FH only */
191 /*66*/	u_int8_t	ns_fhindex;	/* FH only */
192 /*68*/
193 #endif
194 	u_int i, ifindex;
195 	size_t namelen, nodes_len, totallen;
196 	int name[12];
197 	int *vname;
198 	char ifname[IFNAMSIZ];
199 	struct ieee80211_node_sysctl *pns, *ns;
200 	u_int64_t ts;
201 
202 	namelen = __arraycount(name);
203 
204 	if (sysctlnametomib("net.link.ieee80211.nodes", &name[0],
205 	    &namelen) != 0) {
206 		warn("sysctlnametomib");
207 		return -1;
208 	}
209 
210 	if (ifname_arg == NULL)
211 		ifindex = 0;
212 	else if ((ifindex = if_nametoindex(ifname_arg)) == 0) {
213 		warn("if_nametoindex");
214 		return -1;
215 	}
216 
217 	totallen = namelen + IEEE80211_SYSCTL_NODENAMELEN;
218 	if (totallen >= __arraycount(name)) {
219 		warnx("Internal error finding sysctl mib");
220 		return -1;
221 	}
222 	vname = &name[namelen];
223 
224 	vname[IEEE80211_SYSCTL_NODENAME_IF] = ifindex;
225 	vname[IEEE80211_SYSCTL_NODENAME_OP] = IEEE80211_SYSCTL_OP_ALL;
226 	vname[IEEE80211_SYSCTL_NODENAME_ARG] = 0;
227 	vname[IEEE80211_SYSCTL_NODENAME_TYPE] = hdr_type;
228 	vname[IEEE80211_SYSCTL_NODENAME_ELTSIZE] = sizeof(*ns);
229 	vname[IEEE80211_SYSCTL_NODENAME_ELTCOUNT] = INT_MAX;
230 
231 	/* how many? */
232 	if (sysctl(name, totallen, NULL, &nodes_len, NULL, 0) != 0) {
233 		warn("sysctl(count)");
234 		return -1;
235 	}
236 
237 	ns = malloc(nodes_len);
238 
239 	if (ns == NULL) {
240 		warn("malloc");
241 		return -1;
242 	}
243 
244 	vname[IEEE80211_SYSCTL_NODENAME_ELTCOUNT] = nodes_len / sizeof(ns[0]);
245 
246 	/* Get them. */
247 	if (sysctl(name, totallen, ns, &nodes_len, NULL, 0) != 0) {
248 		warn("sysctl(get)");
249 		return -1;
250 	}
251 
252 	for (i = 0; i < nodes_len / sizeof(ns[0]); i++) {
253 		pns = &ns[i];
254 		if (if_indextoname(pns->ns_ifindex, ifname) == NULL) {
255 			warn("if_indextoname");
256 			return -1;
257 		}
258 		if (cf->cf_p && (pns->ns_capinfo & IEEE80211_CAPINFO_PRIVACY))
259 		  	continue;
260 		printf("%s: mac %s ", ifname, ether_string(pns->ns_macaddr));
261 		printf("bss %s\n", ether_string(pns->ns_bssid));
262 		print_node_flags(pns->ns_flags);
263 
264 		/* TBD deal with binary ESSID */
265 		printf("\tess <%.*s>\n", pns->ns_esslen, pns->ns_essid);
266 
267 		print_channel(pns->ns_chanidx, pns->ns_freq, pns->ns_chanflags);
268 
269 		print_capinfo(pns->ns_capinfo);
270 
271 		assert(sizeof(ts) == sizeof(pns->ns_tstamp));
272 		memcpy(&ts, &pns->ns_tstamp[0], sizeof(ts));
273 		printf("\tbeacon-interval %d TU tsft %" PRIu64 " us\n",
274 		    pns->ns_intval, (u_int64_t)le64toh(ts));
275 
276 		print_rateset(&pns->ns_rates, pns->ns_txrate);
277 
278 		printf("\tassoc-id %d assoc-failed %d inactivity %ds\n",
279 		    pns->ns_associd, pns->ns_fails, pns->ns_inact);
280 
281 		printf("\trssi %d txseq %d rxseq %d\n",
282 		    pns->ns_rssi, pns->ns_txseq, pns->ns_rxseq);
283 	}
284 	return 0;
285 }
286 
287 static void
usage(void)288 usage(void)
289 {
290 	fprintf(stderr,
291 	    "Usage: %s [ -p ] -a\n"
292 	    "       %s [ -p ] interface [ interface ... ]\n",
293 	    getprogname(), getprogname());
294 	exit(EXIT_FAILURE);
295 }
296 
297 static void
parse_args(int * argcp,char *** argvp,struct cmdflags * cf)298 parse_args(int *argcp, char ***argvp, struct cmdflags *cf)
299 {
300 	int ch;
301 
302 	(void)memset(cf, 0, sizeof(*cf));
303 
304 	while ((ch = getopt(*argcp, *argvp, "ap")) != -1) {
305 		switch (ch) {
306 		case 'a':
307 			cf->cf_a = 1;
308 			break;
309 		case 'p':
310 			cf->cf_p = 1;
311 			break;
312 		default:
313 			warnx("unknown option -%c", ch);
314 			usage();
315 		}
316 	}
317 
318 	*argcp -= optind;
319 	*argvp += optind;
320 }
321 
322 #define	LOGICAL_XOR(x, y) (!(x) != !(y))
323 
324 int
main(int argc,char ** argv)325 main(int argc, char **argv)
326 {
327 	int i;
328 	struct cmdflags cf;
329 
330 	parse_args(&argc, &argv, &cf);
331 
332 	if (!LOGICAL_XOR(argc > 0, cf.cf_a))
333 		usage();
334 
335 	if (cf.cf_a) {
336 		if (dump_nodes(NULL, IEEE80211_SYSCTL_T_NODE, &cf) != 0)
337 			return EXIT_FAILURE;
338 		return EXIT_SUCCESS;
339 	}
340 	for (i = 0; i < argc; i++) {
341 		if (dump_nodes(argv[i], IEEE80211_SYSCTL_T_NODE, &cf) != 0)
342 			return EXIT_FAILURE;
343 	}
344 
345 	return EXIT_SUCCESS;
346 }
347