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