summaryrefslogtreecommitdiffstats
path: root/freebsd/tools/tools/net80211/wlanstats/main.c
blob: 3a6fd20489fe6acbc1155e7415cec853b891aa2b (plain) (blame)
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
349
350
351
352
353
354
#include <machine/rtems-bsd-user-space.h>

#ifdef __rtems__
#include "rtems-bsd-wlanstats-namespace.h"
#endif /* __rtems__ */

/*-
 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
 * 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,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 *    redistribution must be conditioned upon including a substantially
 *    similar Disclaimer requirement for further binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 DAMAGES.
 *
 * $FreeBSD$
 */

/*
 * wlanstats [-i interface]
 * (default interface is wlan0).
 */

#ifdef __rtems__
#define __need_getopt_newlib
#include <getopt.h>
#include <machine/rtems-bsd-program.h>
#include <machine/rtems-bsd-commands.h>
#endif /* __rtems__ */
#include <sys/param.h>
#include <sys/socket.h>

#include <net/ethernet.h>
#include <net80211/_ieee80211.h>

#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

#include "wlanstats.h"
#ifdef __rtems__
#include "rtems-bsd-wlanstats-main-data.h"
#endif /* __rtems__ */

#ifndef __rtems__
static struct {
#else /* __rtems__ */
static const struct {
#endif /* __rtems__ */
	const char *tag;
	const char *fmt;
} tags[] = {
  { "default",
    "input,rx_mgmt,output,rx_badkeyid,scan_active,scan_bg,bmiss,rssi,noise,rate"
  },
  { "ampdu",
    "input,output,ampdu_reorder,ampdu_oor,rx_dup,ampdu_flush,ampdu_move,"
    "ampdu_drop,ampdu_bar,ampdu_baroow,ampdu_barmove,ampdu_bartx,"
    "ampdu_bartxfail,ampdu_bartxretry,rssi,rate"
  },
  {
    "amsdu",
    "input,output,amsdu_tooshort,amsdu_split,amsdu_decap,amsdu_encap,rssi,rate"
  },
};

static const char *
getfmt(const char *tag)
{
	int i;
	for (i = 0; i < nitems(tags); i++)
		if (strcasecmp(tags[i].tag, tag) == 0)
			return tags[i].fmt;
	return tag;
}

static int signalled;

static void
catchalarm(int signo __unused)
{
	signalled = 1;
}

#if 0
static void
print_sta_stats(FILE *fd, const u_int8_t macaddr[IEEE80211_ADDR_LEN])
{
#define	STAT(x,fmt) \
	if (ns->ns_##x) { fprintf(fd, "%s" #x " " fmt, sep, ns->ns_##x); sep = " "; }
	struct ieee80211req ireq;
	struct ieee80211req_sta_stats stats;
	const struct ieee80211_nodestats *ns = &stats.is_stats;
	const char *sep;

	(void) memset(&ireq, 0, sizeof(ireq));
	(void) strncpy(ireq.i_name, ifr.ifr_name, sizeof(ireq.i_name));
	ireq.i_type = IEEE80211_IOC_STA_STATS;
	ireq.i_data = &stats;
	ireq.i_len = sizeof(stats);
	memcpy(stats.is_u.macaddr, macaddr, IEEE80211_ADDR_LEN);
	if (ioctl(s, SIOCG80211, &ireq) < 0)
		err(1, "unable to get station stats for %s",
			ether_ntoa((const struct ether_addr*) macaddr));

	fprintf(fd, "%s:\n", ether_ntoa((const struct ether_addr*) macaddr));

	sep = "\t";
	STAT(rx_data, "%u");
	STAT(rx_mgmt, "%u");
	STAT(rx_ctrl, "%u");
	STAT(rx_beacons, "%u");
	STAT(rx_proberesp, "%u");
	STAT(rx_ucast, "%u");
	STAT(rx_mcast, "%u");
	STAT(rx_bytes, "%llu");
	STAT(rx_dup, "%u");
	STAT(rx_noprivacy, "%u");
	STAT(rx_wepfail, "%u");
	STAT(rx_demicfail, "%u");
	STAT(rx_decap, "%u");
	STAT(rx_defrag, "%u");
	STAT(rx_disassoc, "%u");
	STAT(rx_deauth, "%u");
	STAT(rx_decryptcrc, "%u");
	STAT(rx_unauth, "%u");
	STAT(rx_unencrypted, "%u");
	fprintf(fd, "\n");

	sep = "\t";
	STAT(tx_data, "%u");
	STAT(tx_mgmt, "%u");
	STAT(tx_probereq, "%u");
	STAT(tx_ucast, "%u");
	STAT(tx_mcast, "%u");
	STAT(tx_bytes, "%llu");
	STAT(tx_novlantag, "%u");
	STAT(tx_vlanmismatch, "%u");
	fprintf(fd, "\n");

	sep = "\t";
	STAT(tx_assoc, "%u");
	STAT(tx_assoc_fail, "%u");
	STAT(tx_auth, "%u");
	STAT(tx_auth_fail, "%u");
	STAT(tx_deauth, "%u");
	STAT(tx_deauth_code, "%llu");
	STAT(tx_disassoc, "%u");
	STAT(tx_disassoc_code, "%u");
	fprintf(fd, "\n");

#undef STAT
}
#endif

void
usage(void) {
	printf("wlanstats: [-ah] [-i ifname] [-l] [-m station MAC address] [-o fmt] [interval]\n");
}

#ifdef __rtems__
static int main(int argc, char *argv[]);

RTEMS_LINKER_RWSET(bsd_prog_wlanstats, char);

int
rtems_bsd_command_wlanstats(int argc, char *argv[])
{
	int exit_code;
	void *data_begin;
	size_t data_size;

	data_begin = RTEMS_LINKER_SET_BEGIN(bsd_prog_wlanstats);
	data_size = RTEMS_LINKER_SET_SIZE(bsd_prog_wlanstats);

	rtems_bsd_program_lock();
	exit_code = rtems_bsd_program_call_main_with_data_restore("wlanstats",
	    main, argc, argv, data_begin, data_size);
	rtems_bsd_program_unlock();

	return exit_code;
}
#endif /* __rtems__ */
int
main(int argc, char *argv[])
{
	struct wlanstatfoo *wf;
	struct ether_addr *ea;
	const uint8_t *mac = NULL;
	const char *ifname;
	int allnodes = 0;
	int c, mode;
#ifdef __rtems__
	struct getopt_data getopt_data;
	memset(&getopt_data, 0, sizeof(getopt_data));
#define optind getopt_data.optind
#define optarg getopt_data.optarg
#define opterr getopt_data.opterr
#define optopt getopt_data.optopt
#define getopt(argc, argv, opt) getopt_r(argc, argv, "+" opt, &getopt_data)
#endif /* __rtems__ */

	ifname = getenv("WLAN");
	if (ifname == NULL)
		ifname = "wlan0";
	wf = wlanstats_new(ifname, getfmt("default"));
	while ((c = getopt(argc, argv, "ahi:lm:o:")) != -1) {
		switch (c) {
		case 'a':
			allnodes++;
			break;
		case 'h':
			usage();
			exit(0);
		case 'i':
			wf->setifname(wf, optarg);
			break;
		case 'l':
			wf->print_fields(wf, stdout);
			return 0;
		case 'm':
			ea = ether_aton(optarg);
			if (!ea)
				errx(1, "%s: invalid ethernet address", optarg);
			mac = ea->octet;
			break;
		case 'o':
			wf->setfmt(wf, getfmt(optarg));
			break;
		default:
			usage();
			exit(1);
			/*NOTREACHED*/
		}
	}
	argc -= optind;
	argv += optind;

	mode = wf->getopmode(wf);
	wf->setstamac(wf, mac);

	if (argc > 0) {
		u_long interval = strtoul(argv[0], NULL, 0);
		int line, omask;

		if (interval < 1)
			interval = 1;
		signal(SIGALRM, catchalarm);
		signalled = 0;
		alarm(interval);
	banner:
		wf->print_header(wf, stdout);
		line = 0;
	loop:
		if (line != 0) {
			wf->collect_cur(wf);
			wf->print_current(wf, stdout);
			wf->update_tot(wf);
		} else {
			wf->collect_tot(wf);
			wf->print_total(wf, stdout);
		}
		fflush(stdout);
#ifndef __rtems__
		omask = sigblock(sigmask(SIGALRM));
		if (!signalled)
			sigpause(0);
		sigsetmask(omask);
#else /* __rtems__ */
		{
		sigset_t oldmask, desired, empty;

		sigemptyset(&empty);
		sigemptyset(&desired);
		sigaddset(&desired, SIGALRM);
		sigprocmask(SIG_BLOCK, &desired, &oldmask);
		while (!signalled)
			sigsuspend(&desired);
		sigprocmask(SIG_SETMASK, &oldmask, NULL);
		}
#endif /* __rtems__ */
		signalled = 0;
		alarm(interval);
		line++;
		/* refresh every display in case sta roams */
		if (mac == NULL && mode == IEEE80211_M_STA)
			wf->setstamac(wf, NULL);
		if (line == 21)		/* XXX tty line count */
			goto banner;
		else
			goto loop;
		/*NOTREACHED*/
#if 0
	} else if (allnodes) {
		struct ieee80211req_sta_info *si;
		union {
			struct ieee80211req_sta_req req;
			uint8_t buf[24*1024];
		} u;
		uint8_t *cp;
		struct ieee80211req ireq;
		int len;

		/*
		 * Retrieve station/neighbor table and print stats for each.
		 */
		(void) memset(&ireq, 0, sizeof(ireq));
		(void) strncpy(ireq.i_name, ifr.ifr_name, sizeof(ireq.i_name));
		ireq.i_type = IEEE80211_IOC_STA_INFO;
		memset(&u.req.macaddr, 0xff, sizeof(u.req.macaddr));
		ireq.i_data = &u;
		ireq.i_len = sizeof(u);
		if (ioctl(s, SIOCG80211, &ireq) < 0)
			err(1, "unable to get station information");
		len = ireq.i_len;
		if (len >= sizeof(struct ieee80211req_sta_info)) {
			cp = u.req.info;
			do {
				si = (struct ieee80211req_sta_info *) cp;
				if (si->isi_len < sizeof(*si))
					break;
				print_sta_stats(stdout, si->isi_macaddr);
				cp += si->isi_len, len -= si->isi_len;
			} while (len >= sizeof(struct ieee80211req_sta_info));
		}
#endif
	} else {
		wf->collect_tot(wf);
		wf->print_verbose(wf, stdout);
	}
	return 0;
}