xref: /freebsd/tools/tools/net80211/wlanstats/main.c (revision 19261079)
1 /*-
2  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * wlanstats [-i interface]
34  * (default interface is wlan0).
35  */
36 
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 
40 #include <net/ethernet.h>
41 #include <net80211/_ieee80211.h>
42 
43 #include <err.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <strings.h>
48 #include <unistd.h>
49 
50 #include "wlanstats.h"
51 
52 static struct {
53 	const char *tag;
54 	const char *fmt;
55 } tags[] = {
56   { "default",
57     "input,rx_mgmt,output,rx_badkeyid,scan_active,scan_bg,bmiss,rssi,noise,rate"
58   },
59   { "ampdu",
60     "input,output,ampdu_reorder,ampdu_oor,rx_dup,ampdu_flush,ampdu_move,"
61     "ampdu_drop,ampdu_bar,ampdu_baroow,ampdu_barmove,ampdu_bartx,"
62     "ampdu_bartxfail,ampdu_bartxretry,rssi,rate"
63   },
64   {
65     "amsdu",
66     "input,output,amsdu_tooshort,amsdu_split,amsdu_decap,amsdu_encap,rx_amsdu_more,rx_amsdu_more_end,rssi,rate"
67   },
68 };
69 
70 static const char *
71 getfmt(const char *tag)
72 {
73 	unsigned int i;
74 	for (i = 0; i < nitems(tags); i++)
75 		if (strcasecmp(tags[i].tag, tag) == 0)
76 			return tags[i].fmt;
77 	return tag;
78 }
79 
80 static int signalled;
81 
82 static void
83 catchalarm(int signo __unused)
84 {
85 	signalled = 1;
86 }
87 
88 #if 0
89 static void
90 print_sta_stats(FILE *fd, const u_int8_t macaddr[IEEE80211_ADDR_LEN])
91 {
92 #define	STAT(x,fmt) \
93 	if (ns->ns_##x) { fprintf(fd, "%s" #x " " fmt, sep, ns->ns_##x); sep = " "; }
94 	struct ieee80211req ireq;
95 	struct ieee80211req_sta_stats stats;
96 	const struct ieee80211_nodestats *ns = &stats.is_stats;
97 	const char *sep;
98 
99 	(void) memset(&ireq, 0, sizeof(ireq));
100 	(void) strncpy(ireq.i_name, ifr.ifr_name, sizeof(ireq.i_name));
101 	ireq.i_type = IEEE80211_IOC_STA_STATS;
102 	ireq.i_data = &stats;
103 	ireq.i_len = sizeof(stats);
104 	memcpy(stats.is_u.macaddr, macaddr, IEEE80211_ADDR_LEN);
105 	if (ioctl(s, SIOCG80211, &ireq) < 0)
106 		err(1, "unable to get station stats for %s",
107 			ether_ntoa((const struct ether_addr*) macaddr));
108 
109 	fprintf(fd, "%s:\n", ether_ntoa((const struct ether_addr*) macaddr));
110 
111 	sep = "\t";
112 	STAT(rx_data, "%u");
113 	STAT(rx_mgmt, "%u");
114 	STAT(rx_ctrl, "%u");
115 	STAT(rx_beacons, "%u");
116 	STAT(rx_proberesp, "%u");
117 	STAT(rx_ucast, "%u");
118 	STAT(rx_mcast, "%u");
119 	STAT(rx_bytes, "%llu");
120 	STAT(rx_dup, "%u");
121 	STAT(rx_noprivacy, "%u");
122 	STAT(rx_wepfail, "%u");
123 	STAT(rx_demicfail, "%u");
124 	STAT(rx_decap, "%u");
125 	STAT(rx_defrag, "%u");
126 	STAT(rx_disassoc, "%u");
127 	STAT(rx_deauth, "%u");
128 	STAT(rx_decryptcrc, "%u");
129 	STAT(rx_unauth, "%u");
130 	STAT(rx_unencrypted, "%u");
131 	fprintf(fd, "\n");
132 
133 	sep = "\t";
134 	STAT(tx_data, "%u");
135 	STAT(tx_mgmt, "%u");
136 	STAT(tx_probereq, "%u");
137 	STAT(tx_ucast, "%u");
138 	STAT(tx_mcast, "%u");
139 	STAT(tx_bytes, "%llu");
140 	STAT(tx_novlantag, "%u");
141 	STAT(tx_vlanmismatch, "%u");
142 	fprintf(fd, "\n");
143 
144 	sep = "\t";
145 	STAT(tx_assoc, "%u");
146 	STAT(tx_assoc_fail, "%u");
147 	STAT(tx_auth, "%u");
148 	STAT(tx_auth_fail, "%u");
149 	STAT(tx_deauth, "%u");
150 	STAT(tx_deauth_code, "%llu");
151 	STAT(tx_disassoc, "%u");
152 	STAT(tx_disassoc_code, "%u");
153 	fprintf(fd, "\n");
154 
155 #undef STAT
156 }
157 #endif
158 
159 static void
160 usage(void)
161 {
162 
163 	printf("wlanstats: [-ah] [-i ifname] [-l] [-m station MAC address] [-o fmt] [interval]\n");
164 }
165 
166 int
167 main(int argc, char *argv[])
168 {
169 	struct wlanstatfoo *wf;
170 	struct ether_addr *ea;
171 	const uint8_t *mac = NULL;
172 	const char *ifname;
173 	int allnodes = 0;
174 	int c, mode;
175 
176 	ifname = getenv("WLAN");
177 	if (ifname == NULL)
178 		ifname = "wlan0";
179 	wf = wlanstats_new(ifname, getfmt("default"));
180 	while ((c = getopt(argc, argv, "ahi:lm:o:")) != -1) {
181 		switch (c) {
182 		case 'a':
183 			allnodes++;
184 			break;
185 		case 'h':
186 			usage();
187 			exit(0);
188 		case 'i':
189 			wf->setifname(wf, optarg);
190 			break;
191 		case 'l':
192 			wf->print_fields(wf, stdout);
193 			return 0;
194 		case 'm':
195 			ea = ether_aton(optarg);
196 			if (!ea)
197 				errx(1, "%s: invalid ethernet address", optarg);
198 			mac = ea->octet;
199 			break;
200 		case 'o':
201 			wf->setfmt(wf, getfmt(optarg));
202 			break;
203 		default:
204 			usage();
205 			exit(1);
206 			/*NOTREACHED*/
207 		}
208 	}
209 	argc -= optind;
210 	argv += optind;
211 
212 	mode = wf->getopmode(wf);
213 	wf->setstamac(wf, mac);
214 
215 	if (argc > 0) {
216 		u_long interval = strtoul(argv[0], NULL, 0);
217 		int line, omask;
218 
219 		if (interval < 1)
220 			interval = 1;
221 		signal(SIGALRM, catchalarm);
222 		signalled = 0;
223 		alarm(interval);
224 	banner:
225 		wf->print_header(wf, stdout);
226 		line = 0;
227 	loop:
228 		if (line != 0) {
229 			wf->collect_cur(wf);
230 			wf->print_current(wf, stdout);
231 			wf->update_tot(wf);
232 		} else {
233 			wf->collect_tot(wf);
234 			wf->print_total(wf, stdout);
235 		}
236 		fflush(stdout);
237 		omask = sigblock(sigmask(SIGALRM));
238 		if (!signalled)
239 			sigpause(0);
240 		sigsetmask(omask);
241 		signalled = 0;
242 		alarm(interval);
243 		line++;
244 		/* refresh every display in case sta roams */
245 		if (mac == NULL && mode == IEEE80211_M_STA)
246 			wf->setstamac(wf, NULL);
247 		if (line == 21)		/* XXX tty line count */
248 			goto banner;
249 		else
250 			goto loop;
251 		/*NOTREACHED*/
252 #if 0
253 	} else if (allnodes) {
254 		struct ieee80211req_sta_info *si;
255 		union {
256 			struct ieee80211req_sta_req req;
257 			uint8_t buf[24*1024];
258 		} u;
259 		uint8_t *cp;
260 		struct ieee80211req ireq;
261 		int len;
262 
263 		/*
264 		 * Retrieve station/neighbor table and print stats for each.
265 		 */
266 		(void) memset(&ireq, 0, sizeof(ireq));
267 		(void) strncpy(ireq.i_name, ifr.ifr_name, sizeof(ireq.i_name));
268 		ireq.i_type = IEEE80211_IOC_STA_INFO;
269 		memset(&u.req.macaddr, 0xff, sizeof(u.req.macaddr));
270 		ireq.i_data = &u;
271 		ireq.i_len = sizeof(u);
272 		if (ioctl(s, SIOCG80211, &ireq) < 0)
273 			err(1, "unable to get station information");
274 		len = ireq.i_len;
275 		if (len >= sizeof(struct ieee80211req_sta_info)) {
276 			cp = u.req.info;
277 			do {
278 				si = (struct ieee80211req_sta_info *) cp;
279 				if (si->isi_len < sizeof(*si))
280 					break;
281 				print_sta_stats(stdout, si->isi_macaddr);
282 				cp += si->isi_len, len -= si->isi_len;
283 			} while (len >= sizeof(struct ieee80211req_sta_info));
284 		}
285 #endif
286 	} else {
287 		wf->collect_tot(wf);
288 		wf->print_verbose(wf, stdout);
289 	}
290 	return 0;
291 }
292