xref: /freebsd/tools/tools/net80211/wlanstats/wlanstats.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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  * net80211 statistics class.
34  */
35 #include <sys/types.h>
36 #include <sys/file.h>
37 #include <sys/sockio.h>
38 #include <sys/socket.h>
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 #include <net/if_var.h>
43 #include <net/ethernet.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <signal.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <err.h>
51 #include <ifaddrs.h>
52 
53 #include "../../../../sys/net80211/ieee80211_ioctl.h"
54 
55 #include "wlanstats.h"
56 
57 #ifndef IEEE80211_ADDR_COPY
58 #define	IEEE80211_ADDR_COPY(dst, src)	memcpy(dst, src, IEEE80211_ADDR_LEN)
59 #define	IEEE80211_ADDR_EQ(a1,a2)	(memcmp(a1,a2,IEEE80211_ADDR_LEN) == 0)
60 #endif
61 
62 #define	AFTER(prev)	((prev)+1)
63 
64 static const struct fmt wlanstats[] = {
65 #define	S_RX_BADVERSION		0
66 	{ 5,  "rx_badversion",	"bvers",	"rx frame with bad version" },
67 #define	S_RX_TOOSHORT		AFTER(S_RX_BADVERSION)
68 	{ 5,  "rx_tooshort",	"2short",	"rx frame too short" },
69 #define	S_RX_WRONGBSS		AFTER(S_RX_TOOSHORT)
70 	{ 5,  "rx_wrongbss",	"wrbss",	"rx from wrong bssid" },
71 #define	S_RX_DUP		AFTER(S_RX_WRONGBSS)
72 	{ 5,  "rx_dup",		"rxdup",	"rx discard 'cuz dup" },
73 #define	S_RX_WRONGDIR		AFTER(S_RX_DUP)
74 	{ 5,  "rx_wrongdir",	"wrdir",	"rx w/ wrong direction" },
75 #define	S_RX_MCASTECHO		AFTER(S_RX_WRONGDIR)
76 	{ 5,  "rx_mcastecho",	"mecho",	"rx discard 'cuz mcast echo" },
77 #define	S_RX_NOTASSOC		AFTER(S_RX_MCASTECHO)
78 	{ 6,  "rx_notassoc",	"!assoc",	"rx discard 'cuz sta !assoc" },
79 #define	S_RX_NOPRIVACY		AFTER(S_RX_NOTASSOC)
80 	{ 6,  "rx_noprivacy",	"nopriv",	"rx w/ wep but privacy off" },
81 #define	S_RX_UNENCRYPTED	AFTER(S_RX_NOPRIVACY)
82 	{ 6,  "rx_unencrypted",	"unencr",	"rx w/o wep and privacy on" },
83 #define	S_RX_WEPFAIL		AFTER(S_RX_UNENCRYPTED)
84 	{ 7,  "rx_wepfail",	"wepfail",	"rx wep processing failed" },
85 #define	S_RX_DECAP		AFTER(S_RX_WEPFAIL)
86 	{ 5,  "rx_decap",	"decap",	"rx decapsulation failed" },
87 #define	S_RX_MGTDISCARD		AFTER(S_RX_DECAP)
88 	{ 8,  "rx_mgtdiscard",	"mgtdiscard",	"rx discard mgt frames" },
89 #define	S_RX_CTL		AFTER(S_RX_MGTDISCARD)
90 	{ 5,  "rx_ctl",		"ctl",		"rx ctrl frames" },
91 #define	S_RX_BEACON		AFTER(S_RX_CTL)
92 	{ 6,  "rx_beacon",	"beacon",	"rx beacon frames" },
93 #define	S_RX_RSTOOBIG		AFTER(S_RX_BEACON)
94 	{ 6,  "rx_rstoobig",	"rs2big",	"rx rate set truncated" },
95 #define	S_RX_ELEM_MISSING	AFTER(S_RX_RSTOOBIG)
96 	{ 6,  "rx_elem_missing","iemiss",	"rx required element missing" },
97 #define	S_RX_ELEM_TOOBIG	AFTER(S_RX_ELEM_MISSING)
98 	{ 6,  "rx_elem_toobig",	"ie2big",	"rx element too big" },
99 #define	S_RX_ELEM_TOOSMALL	AFTER(S_RX_ELEM_TOOBIG)
100 	{ 7,  "rx_elem_toosmall","ie2small","rx element too small" },
101 #define	S_RX_ELEM_UNKNOWN 	AFTER(S_RX_ELEM_TOOSMALL)
102 	{ 5,  "rx_elem_unknown","ieunk",	"rx element unknown" },
103 #define	S_RX_BADCHAN		AFTER(S_RX_ELEM_UNKNOWN)
104 	{ 6,  "rx_badchan",	"badchan",	"rx frame w/ invalid chan" },
105 #define	S_RX_CHANMISMATCH	AFTER(S_RX_BADCHAN)
106 	{ 5,  "rx_chanmismatch","chanmismatch",	"rx frame chan mismatch" },
107 #define	S_RX_NODEALLOC		AFTER(S_RX_CHANMISMATCH)
108 	{ 5,  "rx_nodealloc",	"nodealloc",	"nodes allocated (rx)" },
109 #define	S_RX_SSIDMISMATCH	AFTER(S_RX_NODEALLOC)
110 	{ 5,  "rx_ssidmismatch","ssidmismatch",	"rx frame ssid mismatch" },
111 #define	S_RX_AUTH_UNSUPPORTED	AFTER(S_RX_SSIDMISMATCH)
112 	{ 5,  "rx_auth_unsupported","auth_unsupported",
113 		"rx w/ unsupported auth alg" },
114 #define	S_RX_AUTH_FAIL		AFTER(S_RX_AUTH_UNSUPPORTED)
115 	{ 5,  "rx_auth_fail",	"auth_fail",	"rx sta auth failure" },
116 #define	S_RX_AUTH_COUNTERMEASURES	AFTER(S_RX_AUTH_FAIL)
117 	{ 5,  "rx_auth_countermeasures",	"auth_countermeasures",
118 		"rx sta auth failure 'cuz of TKIP countermeasures" },
119 #define	S_RX_ASSOC_BSS		AFTER(S_RX_AUTH_COUNTERMEASURES)
120 	{ 5,  "rx_assoc_bss",	"assoc_bss",	"rx assoc from wrong bssid" },
121 #define	S_RX_ASSOC_NOTAUTH	AFTER(S_RX_ASSOC_BSS)
122 	{ 5,  "rx_assoc_notauth","assoc_notauth",	"rx assoc w/o auth" },
123 #define	S_RX_ASSOC_CAPMISMATCH	AFTER(S_RX_ASSOC_NOTAUTH)
124 	{ 5,  "rx_assoc_capmismatch","assoc_capmismatch",
125 		"rx assoc w/ cap mismatch" },
126 #define	S_RX_ASSOC_NORATE	AFTER(S_RX_ASSOC_CAPMISMATCH)
127 	{ 5,  "rx_assoc_norate","assoc_norate",	"rx assoc w/ no rate match" },
128 #define	S_RX_ASSOC_BADWPAIE	AFTER(S_RX_ASSOC_NORATE)
129 	{ 5,  "rx_assoc_badwpaie","assoc_badwpaie",
130 		"rx assoc w/ bad WPA IE" },
131 #define	S_RX_DEAUTH		AFTER(S_RX_ASSOC_BADWPAIE)
132 	{ 5,  "rx_deauth",	"deauth",	"rx deauthentication" },
133 #define	S_RX_DISASSOC		AFTER(S_RX_DEAUTH)
134 	{ 5,  "rx_disassoc",	"disassoc",	"rx disassociation" },
135 #define	S_RX_BADSUBTYPE		AFTER(S_RX_DISASSOC)
136 	{ 5,  "rx_badsubtype",	"badsubtype",	"rx frame w/ unknown subtype" },
137 #define	S_RX_NOBUF		AFTER(S_RX_BADSUBTYPE)
138 	{ 5,  "rx_nobuf",	"nobuf",	"rx failed for lack of mbuf" },
139 #define	S_RX_DECRYPTCRC		AFTER(S_RX_NOBUF)
140 	{ 5,  "rx_decryptcrc",	"decryptcrc",	"rx decrypt failed on crc" },
141 #define	S_RX_AHDEMO_MGT		AFTER(S_RX_DECRYPTCRC)
142 	{ 5,  "rx_ahdemo_mgt",	"ahdemo_mgt",
143 		"rx discard mgmt frame received in ahdoc demo mode" },
144 #define	S_RX_BAD_AUTH		AFTER(S_RX_AHDEMO_MGT)
145 	{ 5,  "rx_bad_auth",	"bad_auth",	"rx bad authentication request" },
146 #define	S_RX_UNAUTH		AFTER(S_RX_BAD_AUTH)
147 	{ 5,  "rx_unauth",	"unauth",
148 		"rx discard 'cuz port unauthorized" },
149 #define	S_RX_BADKEYID		AFTER(S_RX_UNAUTH)
150 	{ 5,  "rx_badkeyid",	"badkeyid",	"rx w/ incorrect keyid" },
151 #define	S_RX_CCMPREPLAY		AFTER(S_RX_BADKEYID)
152 	{ 5,  "rx_ccmpreplay",	"ccmpreplay",	"rx seq# violation (CCMP)" },
153 #define	S_RX_CCMPFORMAT		AFTER(S_RX_CCMPREPLAY)
154 	{ 5,  "rx_ccmpformat",	"ccmpformat",	"rx format bad (CCMP)" },
155 #define	S_RX_CCMPMIC		AFTER(S_RX_CCMPFORMAT)
156 	{ 5,  "rx_ccmpmic",	"ccmpmic",	"rx MIC check failed (CCMP)" },
157 #define	S_RX_TKIPREPLAY		AFTER(S_RX_CCMPMIC)
158 	{ 5,  "rx_tkipreplay",	"tkipreplay",	"rx seq# violation (TKIP)" },
159 #define	S_RX_TKIPFORMAT		AFTER(S_RX_TKIPREPLAY)
160 	{ 5,  "rx_tkipformat",	"tkipformat",	"rx format bad (TKIP)" },
161 #define	S_RX_TKIPMIC		AFTER(S_RX_TKIPFORMAT)
162 	{ 5,  "rx_tkipmic",	"tkipmic",	"rx MIC check failed (TKIP)" },
163 #define	S_RX_TKIPICV		AFTER(S_RX_TKIPMIC)
164 	{ 5,  "rx_tkipicv",	"tkipicv",	"rx ICV check failed (TKIP)" },
165 #define	S_RX_BADCIPHER		AFTER(S_RX_TKIPICV)
166 	{ 5,  "rx_badcipher",	"badcipher",	"rx failed 'cuz bad cipher/key type" },
167 #define	S_RX_NOCIPHERCTX	AFTER(S_RX_BADCIPHER)
168 	{ 5,  "rx_nocipherctx",	"nocipherctx",	"rx failed 'cuz key/cipher ctx not setup" },
169 #define	S_RX_ACL		AFTER(S_RX_NOCIPHERCTX)
170 	{ 5,  "rx_acl",		"acl",		"rx discard 'cuz acl policy" },
171 #define	S_TX_NOBUF		AFTER(S_RX_ACL)
172 	{ 5,  "tx_nobuf",	"nobuf",	"tx failed for lack of mbuf" },
173 #define	S_TX_NONODE		AFTER(S_TX_NOBUF)
174 	{ 5,  "tx_nonode",	"nonode",	"tx failed for no node" },
175 #define	S_TX_UNKNOWNMGT		AFTER(S_TX_NONODE)
176 	{ 5,  "tx_unknownmgt",	"unknownmgt",	"tx of unknown mgt frame" },
177 #define	S_TX_BADCIPHER		AFTER(S_TX_UNKNOWNMGT)
178 	{ 5,  "tx_badcipher",	"badcipher",	"tx failed 'cuz bad ciper/key type" },
179 #define	S_TX_NODEFKEY		AFTER(S_TX_BADCIPHER)
180 	{ 5,  "tx_nodefkey",	"nodefkey",	"tx failed 'cuz no defkey" },
181 #define	S_TX_NOHEADROOM		AFTER(S_TX_NODEFKEY)
182 	{ 5,  "tx_noheadroom",	"noheadroom",	"tx failed 'cuz no space for crypto hdrs" },
183 #define	S_TX_FRAGFRAMES		AFTER(S_TX_NOHEADROOM)
184 	{ 5,  "tx_fragframes",	"fragframes",	"tx frames fragmented" },
185 #define	S_TX_FRAGS		AFTER(S_TX_FRAGFRAMES)
186 	{ 5,  "tx_frags",	"frags",		"tx frags generated" },
187 #define	S_SCAN_ACTIVE		AFTER(S_TX_FRAGS)
188 	{ 5,  "scan_active",	"scan_active",	"active scans started" },
189 #define	S_SCAN_PASSIVE		AFTER(S_SCAN_ACTIVE)
190 	{ 5,  "scan_passive",	"scan_passive",	"passive scans started" },
191 #define	S_NODE_TIMEOUT		AFTER(S_SCAN_PASSIVE)
192 	{ 5,  "node_timeout",	"node_timeout",	"nodes timed out for inactivity" },
193 #define	S_CRYPTO_NOMEM		AFTER(S_NODE_TIMEOUT)
194 	{ 5,  "crypto_nomem",	"crypto_nomem",	"cipher context malloc failed" },
195 #define	S_CRYPTO_TKIP		AFTER(S_CRYPTO_NOMEM)
196 	{ 5,  "crypto_tkip",	"crypto_tkip",	"tkip crypto done in s/w" },
197 #define	S_CRYPTO_TKIPENMIC	AFTER(S_CRYPTO_TKIP)
198 	{ 5,  "crypto_tkipenmic","crypto_tkipenmic",	"tkip tx MIC done in s/w" },
199 #define	S_CRYPTO_TKIPDEMIC	AFTER(S_CRYPTO_TKIPENMIC)
200 	{ 5,  "crypto_tkipdemic","crypto_tkipdemic",	"tkip rx MIC done in s/w" },
201 #define	S_CRYPTO_TKIPCM		AFTER(S_CRYPTO_TKIPDEMIC)
202 	{ 5,  "crypto_tkipcm",	"crypto_tkipcm",	"tkip dropped frames 'cuz of countermeasures" },
203 #define	S_CRYPTO_CCMP		AFTER(S_CRYPTO_TKIPCM)
204 	{ 5,  "crypto_ccmp",	"crypto_ccmp",	"ccmp crypto done in s/w" },
205 #define	S_CRYPTO_WEP		AFTER(S_CRYPTO_CCMP)
206 	{ 5,  "crypto_wep",	"crypto_wep",	"wep crypto done in s/w" },
207 #define	S_CRYPTO_SETKEY_CIPHER	AFTER(S_CRYPTO_WEP)
208 	{ 5,  "crypto_setkey_cipher",	"crypto_setkey_cipher","setkey failed 'cuz cipher rejected data" },
209 #define	S_CRYPTO_SETKEY_NOKEY	AFTER(S_CRYPTO_SETKEY_CIPHER)
210 	{ 5,  "crypto_setkey_nokey",	"crypto_setkey_nokey","setkey failed 'cuz no key index" },
211 #define	S_CRYPTO_DELKEY		AFTER(S_CRYPTO_SETKEY_NOKEY)
212 	{ 5,  "crypto_delkey",	"crypto_delkey",	"driver key delete failed" },
213 #define	S_CRYPTO_BADCIPHER	AFTER(S_CRYPTO_DELKEY)
214 	{ 5,  "crypto_badcipher","crypto_badcipher",	"setkey failed 'cuz unknown cipher" },
215 #define	S_CRYPTO_NOCIPHER	AFTER(S_CRYPTO_BADCIPHER)
216 	{ 5,  "crypto_nocipher","crypto_nocipher",	"setkey failed 'cuz cipher module unavailable" },
217 #define	S_CRYPTO_ATTACHFAIL	AFTER(S_CRYPTO_NOCIPHER)
218 	{ 5,  "crypto_attachfail","crypto_attachfail",	"setkey failed 'cuz cipher attach failed" },
219 #define	S_CRYPTO_SWFALLBACK	AFTER(S_CRYPTO_ATTACHFAIL)
220 	{ 5,  "crypto_swfallback","crypto_swfallback",	"crypto fell back to s/w implementation" },
221 #define	S_CRYPTO_KEYFAIL	AFTER(S_CRYPTO_SWFALLBACK)
222 	{ 5,  "crypto_keyfail",	"crypto_keyfail",	"setkey failed 'cuz driver key alloc failed" },
223 #define	S_CRYPTO_ENMICFAIL	AFTER(S_CRYPTO_KEYFAIL)
224 	{ 5,  "crypto_enmicfail","crypto_enmicfail",	"enmic failed (may be mbuf exhaustion)" },
225 #define	S_IBSS_CAPMISMATCH	AFTER(S_CRYPTO_ENMICFAIL)
226 	{ 5,  "ibss_capmismatch","ibss_capmismatch",	"ibss merge faied 'cuz capabilities mismatch" },
227 #define	S_IBSS_NORATE		AFTER(S_IBSS_CAPMISMATCH)
228 	{ 5,  "ibss_norate",	"ibss_norate",	"ibss merge faied 'cuz rate set mismatch" },
229 #define	S_PS_UNASSOC		AFTER(S_IBSS_NORATE)
230 	{ 5,  "ps_unassoc",	"ps_unassoc",	"ps-poll received for unassociated station" },
231 #define	S_PS_BADAID		AFTER(S_PS_UNASSOC)
232 	{ 5,  "ps_badaid",	"ps_badaid",	"ps-poll received with invalid association id" },
233 #define	S_PS_QEMPTY		AFTER(S_PS_BADAID)
234 	{ 5,  "ps_qempty",	"ps_qempty",	"ps-poll received with nothing to send" },
235 #define	S_FF_BADHDR		AFTER(S_PS_QEMPTY)
236 	{ 5,  "ff_badhdr",	"ff_badhdr",	"fast frame rx'd w/ bad hdr" },
237 #define	S_FF_TOOSHORT		AFTER(S_FF_BADHDR)
238 	{ 5,  "ff_tooshort",	"ff_tooshort",	"fast frame rx decap error" },
239 #define	S_FF_SPLIT		AFTER(S_FF_TOOSHORT)
240 	{ 5,  "ff_split",	"ff_split",	"fast frame rx split error" },
241 #define	S_FF_DECAP		AFTER(S_FF_SPLIT)
242 	{ 5,  "ff_decap",	"ff_decap",	"fast frames decap'd" },
243 #define	S_FF_ENCAP		AFTER(S_FF_DECAP)
244 	{ 5,  "ff_encap",	"ff_encap",	"fast frames encap'd for tx" },
245 #define	S_FF_ENCAPFAIL		AFTER(S_FF_ENCAP)
246 	{ 5,  "ff_encapfail",	"ff_encapfail",	"fast frames encap failed" },
247 #define	S_RX_BADBINTVAL		AFTER(S_FF_ENCAPFAIL)
248 	{ 5,  "rx_badbintval",	"rx_badbintval","rx frame with bogus beacon interval" },
249 #define	S_RX_MGMT		AFTER(S_RX_BADBINTVAL)
250 	{ 5,  "rx_mgmt",	"rx_mgmt",	"rx management frames" },
251 #define	S_RX_DEMICFAIL		AFTER(S_RX_MGMT)
252 	{ 5,  "rx_demicfail",	"rx_demicfail",	"rx demic failed" },
253 #define	S_RX_DEFRAG		AFTER(S_RX_DEMICFAIL)
254 	{ 5,  "rx_defrag",	"rx_defrag",	"rx defragmentation failed" },
255 #define	S_RX_ACTION		AFTER(S_RX_DEFRAG)
256 	{ 5,  "rx_action",	"rx_action",	"rx action frames" },
257 #define	S_AMSDU_TOOSHORT	AFTER(S_RX_ACTION)
258 	{ 8,  "amsdu_tooshort",	"tooshort","A-MSDU rx decap error" },
259 #define	S_AMSDU_SPLIT		AFTER(S_AMSDU_TOOSHORT)
260 	{ 8,  "amsdu_split",	"split",	"A-MSDU rx failed on frame split" },
261 #define	S_AMSDU_DECAP		AFTER(S_AMSDU_SPLIT)
262 	{ 8,  "amsdu_decap",	"decap",	"A-MSDU frames received" },
263 #define	S_AMSDU_ENCAP		AFTER(S_AMSDU_DECAP)
264 	{ 8,  "amsdu_encap",	"encap",	"A-MSDU frames transmitted" },
265 #define	S_AMPDU_REORDER		AFTER(S_AMSDU_ENCAP)
266 	{ 8,  "ampdu_reorder",	"reorder","A-MPDU frames held in reorder q" },
267 #define	S_AMPDU_FLUSH		AFTER(S_AMPDU_REORDER)
268 	{ 8,  "ampdu_flush",	"flush",	"A-MPDU frames sent up from reorder q" },
269 #define	S_AMPDU_BARBAD		AFTER(S_AMPDU_FLUSH)
270 	{ 6,  "ampdu_barbad",	"barbad",	"A-MPDU BAR rx before ADDBA exchange (or disabled with net.link.ieee80211)" },
271 #define	S_AMPDU_BAROOW		AFTER(S_AMPDU_BARBAD)
272 	{ 6,  "ampdu_baroow",	"baroow",	"A-MPDU BAR rx out of BA window" },
273 #define	S_AMPDU_BARMOVE		AFTER(S_AMPDU_BAROOW)
274 	{ 8,  "ampdu_barmove",	"barmove","A-MPDU BAR rx moved BA window" },
275 #define	S_AMPDU_BAR		AFTER(S_AMPDU_BARMOVE)
276 	{ 8,  "ampdu_bar",	"rxbar",	"A-MPDU BAR rx successful" },
277 #define	S_AMPDU_MOVE		AFTER(S_AMPDU_BAR)
278 	{ 5,  "ampdu_move",	"move",	"A-MPDU frame moved BA window" },
279 #define	S_AMPDU_OOR		AFTER(S_AMPDU_MOVE)
280 	{ 8,  "ampdu_oor",	"oorx",	"A-MPDU frames rx out-of-order" },
281 #define	S_AMPDU_COPY		AFTER(S_AMPDU_OOR)
282 	{ 8,  "ampdu_copy",	"copy",	"A-MPDU rx window slots copied" },
283 #define	S_AMPDU_DROP		AFTER(S_AMPDU_COPY)
284 	{ 5,  "ampdu_drop",	"drop",	"A-MPDU frames discarded for out of range seqno" },
285 #define	S_AMPDU_AGE		AFTER(S_AMPDU_DROP)
286 	{ 5,  "ampdu_age",	"age",	"A-MPDU frames sent up due to old age" },
287 #define	S_AMPDU_STOP		AFTER(S_AMPDU_AGE)
288 	{ 5,  "ampdu_stop",	"stop",	"A-MPDU streams stopped" },
289 #define	S_AMPDU_STOP_FAILED	AFTER(S_AMPDU_STOP)
290 	{ 5,  "ampdu_stop_failed","!stop",	"A-MPDU stop requests failed 'cuz stream not running" },
291 #define	S_ADDBA_REJECT		AFTER(S_AMPDU_STOP_FAILED)
292 	{ 5,  "addba_reject",	"reject",	"ADDBA requests rejected 'cuz A-MPDU rx is disabled" },
293 #define	S_ADDBA_NOREQUEST	AFTER(S_ADDBA_REJECT)
294 	{ 5,  "addba_norequest","norequest","ADDBA response frames discarded because no ADDBA request was pending" },
295 #define	S_ADDBA_BADTOKEN	AFTER(S_ADDBA_NOREQUEST)
296 	{ 5,  "addba_badtoken",	"badtoken","ADDBA response frames discarded 'cuz rx'd dialog token is wrong" },
297 #define	S_TX_BADSTATE		AFTER(S_ADDBA_BADTOKEN)
298 	{ 4,  "tx_badstate",	"badstate",	"tx failed 'cuz vap not in RUN state" },
299 #define	S_TX_NOTASSOC		AFTER(S_TX_BADSTATE)
300 	{ 4,  "tx_notassoc",	"notassoc",	"tx failed 'cuz dest sta not associated" },
301 #define	S_TX_CLASSIFY		AFTER(S_TX_NOTASSOC)
302 	{ 4,  "tx_classify",	"classify",	"tx packet classification failed" },
303 #define	S_DWDS_MCAST		AFTER(S_TX_CLASSIFY)
304 	{ 8,  "dwds_mcast",	"dwds_mcast",	"mcast frame transmitted on dwds vap discarded" },
305 #define	S_DWDS_QDROP		AFTER(S_DWDS_MCAST)
306 	{ 8,  "dwds_qdrop",	"dwds_qdrop",	"4-address frame discarded because dwds pending queue is full" },
307 #define	S_HT_ASSOC_NOHTCAP	AFTER(S_DWDS_QDROP)
308 	{ 4,  "ht_nohtcap",	"ht_nohtcap",	"non-HT station rejected in HT-only BSS" },
309 #define	S_HT_ASSOC_DOWNGRADE	AFTER(S_HT_ASSOC_NOHTCAP)
310 	{ 4,  "ht_downgrade",	"ht_downgrade",	"HT station downgraded to legacy operation" },
311 #define	S_HT_ASSOC_NORATE	AFTER(S_HT_ASSOC_DOWNGRADE)
312 	{ 4,  "ht_norate",	"ht_norate",	"HT station rejected because of HT rate set" },
313 #define	S_INPUT			AFTER(S_HT_ASSOC_NORATE)
314 	{ 8,	"input",	"input",	"total data frames received" },
315 #define	S_RX_UCAST		AFTER(S_INPUT)
316 	{ 8,	"rx_ucast",	"rx_ucast",	"unicast data frames received" },
317 #define	S_RX_MCAST		AFTER(S_RX_UCAST)
318 	{ 8,	"rx_mcast",	"rx_mcast",	"multicast data frames received" },
319 #define	S_OUTPUT		AFTER(S_RX_MCAST)
320 	{ 8,	"output",	"output",	"total data frames transmit" },
321 #define	S_TX_UCAST		AFTER(S_OUTPUT)
322 	{ 8,	"tx_ucast",	"tx_ucast",	"unicast data frames sent" },
323 #define	S_TX_MCAST		AFTER(S_TX_UCAST)
324 	{ 8,	"tx_mcast",	"tx_mcast",	"multicast data frames sent" },
325 #define	S_RATE			AFTER(S_TX_MCAST)
326 	{ 4,	"rate",		"rate",		"current transmit rate" },
327 #define	S_RSSI			AFTER(S_RATE)
328 	{ 4,	"rssi",		"rssi",		"current rssi" },
329 #define	S_NOISE			AFTER(S_RSSI)
330 	{ 4,	"noise",	"noise",	"current noise floor (dBm)" },
331 #define	S_SIGNAL		AFTER(S_NOISE)
332 	{ 4,	"signal",	"sig",		"current signal (dBm)" },
333 };
334 
335 struct wlanstatfoo_p {
336 	struct wlanstatfoo base;
337 	int s;
338 	int opmode;
339 	uint8_t mac[IEEE80211_ADDR_LEN];
340 	struct ifreq ifr;
341 	struct ieee80211_stats cur;
342 	struct ieee80211_stats total;
343 	struct ieee80211req ireq;
344 	union {
345 		struct ieee80211req_sta_req info;
346 		char buf[1024];
347 	} u_info;
348 	struct ieee80211req_sta_stats ncur;
349 	struct ieee80211req_sta_stats ntotal;
350 };
351 
352 static void
353 wlan_setifname(struct wlanstatfoo *wf0, const char *ifname)
354 {
355 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) wf0;
356 
357 	strncpy(wf->ifr.ifr_name, ifname, sizeof (wf->ifr.ifr_name));
358 	strncpy(wf->ireq.i_name, ifname, sizeof (wf->ireq.i_name));
359 }
360 
361 static const char *
362 wlan_getifname(struct wlanstatfoo *wf0)
363 {
364 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) wf0;
365 
366 	return wf->ifr.ifr_name;
367 }
368 
369 static int
370 wlan_getopmode(struct wlanstatfoo *wf0)
371 {
372 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) wf0;
373 
374 	if (wf->opmode == -1) {
375 		struct ifmediareq ifmr;
376 
377 		memset(&ifmr, 0, sizeof(ifmr));
378 		strlcpy(ifmr.ifm_name, wf->ifr.ifr_name, sizeof(ifmr.ifm_name));
379 		if (ioctl(wf->s, SIOCGIFMEDIA, &ifmr) < 0)
380 			err(1, "%s (SIOCGIFMEDIA)", wf->ifr.ifr_name);
381 		if (ifmr.ifm_current & IFM_IEEE80211_ADHOC)
382 			wf->opmode = IEEE80211_M_IBSS;	/* XXX ahdemo */
383 		else if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
384 			wf->opmode = IEEE80211_M_HOSTAP;
385 		else if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
386 			wf->opmode = IEEE80211_M_MONITOR;
387 		else
388 			wf->opmode = IEEE80211_M_STA;
389 	}
390 	return wf->opmode;
391 }
392 
393 static void
394 getlladdr(struct wlanstatfoo_p *wf)
395 {
396 	const struct sockaddr_dl *sdl;
397 	struct ifaddrs *ifp, *p;
398 
399 	if (getifaddrs(&ifp) != 0)
400 		err(1, "getifaddrs");
401 	for (p = ifp; p != NULL; p = p->ifa_next)
402 		if (strcmp(p->ifa_name, wf->ifr.ifr_name) == 0 &&
403 		    p->ifa_addr->sa_family == AF_LINK)
404 			break;
405 	if (p == NULL)
406 		errx(1, "did not find link layer address for interface %s",
407 			wf->ifr.ifr_name);
408 	sdl = (const struct sockaddr_dl *) p->ifa_addr;
409 	IEEE80211_ADDR_COPY(wf->mac, LLADDR(sdl));
410 	freeifaddrs(ifp);
411 }
412 
413 static void
414 wlan_setstamac(struct wlanstatfoo *wf0, const uint8_t *mac)
415 {
416 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) wf0;
417 
418 	if (mac == NULL) {
419 		switch (wlan_getopmode(wf0)) {
420 		case IEEE80211_M_HOSTAP:
421 		case IEEE80211_M_MONITOR:
422 		case IEEE80211_M_IBSS:
423 		case IEEE80211_M_AHDEMO:
424 			getlladdr(wf);
425 			break;
426 		case IEEE80211_M_STA:
427 			wf->ireq.i_type = IEEE80211_IOC_BSSID;
428 			wf->ireq.i_data = wf->mac;
429 			wf->ireq.i_len = IEEE80211_ADDR_LEN;
430 			if (ioctl(wf->s, SIOCG80211, &wf->ireq) <0)
431 				err(1, "%s (IEEE80211_IOC_BSSID)", wf->ireq.i_name);
432 			break;
433 		}
434 	} else
435 		IEEE80211_ADDR_COPY(wf->mac, mac);
436 }
437 
438 /* XXX only fetch what's needed to do reports */
439 static void
440 wlan_collect(struct wlanstatfoo_p *wf,
441 	struct ieee80211_stats *stats, struct ieee80211req_sta_stats *nstats)
442 {
443 
444 	IEEE80211_ADDR_COPY(wf->u_info.info.is_u.macaddr, wf->mac);
445 	wf->ireq.i_type = IEEE80211_IOC_STA_INFO;
446 	wf->ireq.i_data = (caddr_t) &wf->u_info;
447 	wf->ireq.i_len = sizeof(wf->u_info);
448 	if (ioctl(wf->s, SIOCG80211, &wf->ireq) < 0)
449 		err(1, "%s (IEEE80211_IOC_STA_INFO)", wf->ireq.i_name);
450 
451 	IEEE80211_ADDR_COPY(nstats->is_u.macaddr, wf->mac);
452 	wf->ireq.i_type = IEEE80211_IOC_STA_STATS;
453 	wf->ireq.i_data = (caddr_t) nstats;
454 	wf->ireq.i_len = sizeof(*nstats);
455 	if (ioctl(wf->s, SIOCG80211, &wf->ireq) < 0)
456 		err(1, "%s (IEEE80211_IOC_STA_STATS)", wf->ireq.i_name);
457 
458 	wf->ifr.ifr_data = (caddr_t) stats;
459 	if (ioctl(wf->s, SIOCG80211STATS, &wf->ifr) < 0)
460 		err(1, "%s (SIOCG80211STATS)", wf->ifr.ifr_name);
461 }
462 
463 static void
464 wlan_collect_cur(struct statfoo *sf)
465 {
466 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) sf;
467 
468 	wlan_collect(wf, &wf->cur, &wf->ncur);
469 }
470 
471 static void
472 wlan_collect_tot(struct statfoo *sf)
473 {
474 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) sf;
475 
476 	wlan_collect(wf, &wf->total, &wf->ntotal);
477 }
478 
479 static void
480 wlan_update_tot(struct statfoo *sf)
481 {
482 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) sf;
483 
484 	wf->total = wf->cur;
485 	wf->ntotal = wf->ncur;
486 }
487 
488 static int
489 wlan_getinfo(struct wlanstatfoo_p *wf, int s, char b[], size_t bs)
490 {
491 	const struct ieee80211req_sta_info *si = &wf->u_info.info.info[0];
492 	uint8_t r;
493 
494 	switch (s) {
495 	case S_RATE:
496 		r = si->isi_rates[si->isi_txrate];
497 		snprintf(b, bs, "%uM", (r &~ 0x80) / 2);
498 		return 1;
499 	case S_RSSI:
500 		snprintf(b, bs, "%d", si->isi_rssi);
501 		return 1;
502 	case S_NOISE:
503 		snprintf(b, bs, "%d", si->isi_noise);
504 		return 1;
505 	case S_SIGNAL:
506 		snprintf(b, bs, "%d", si->isi_rssi + si->isi_noise);
507 		return 1;
508 	}
509 	b[0] = '\0';
510 	return 0;
511 }
512 
513 static int
514 wlan_get_curstat(struct statfoo *sf, int s, char b[], size_t bs)
515 {
516 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) sf;
517 #define	STAT(x) \
518 	snprintf(b, bs, "%u", wf->cur.is_##x - wf->total.is_##x); return 1
519 #define	NSTAT(x) \
520 	snprintf(b, bs, "%u", \
521 	    wf->ncur.is_stats.ns_##x - wf->ntotal.is_stats.ns_##x); \
522 	    return 1
523 
524 	switch (s) {
525 	case S_RX_BADVERSION:	STAT(rx_badversion);
526 	case S_RX_TOOSHORT:	STAT(rx_tooshort);
527 	case S_RX_WRONGBSS:	STAT(rx_wrongbss);
528 	case S_RX_DUP:		STAT(rx_dup);
529 	case S_RX_WRONGDIR:	STAT(rx_wrongdir);
530 	case S_RX_MCASTECHO:	STAT(rx_mcastecho);
531 	case S_RX_NOTASSOC:	STAT(rx_notassoc);
532 	case S_RX_NOPRIVACY:	STAT(rx_noprivacy);
533 	case S_RX_UNENCRYPTED:	STAT(rx_unencrypted);
534 	case S_RX_WEPFAIL:	STAT(rx_wepfail);
535 	case S_RX_DECAP:	STAT(rx_decap);
536 	case S_RX_MGTDISCARD:	STAT(rx_mgtdiscard);
537 	case S_RX_CTL:		STAT(rx_ctl);
538 	case S_RX_BEACON:	STAT(rx_beacon);
539 	case S_RX_RSTOOBIG:	STAT(rx_rstoobig);
540 	case S_RX_ELEM_MISSING:	STAT(rx_elem_missing);
541 	case S_RX_ELEM_TOOBIG:	STAT(rx_elem_toobig);
542 	case S_RX_ELEM_TOOSMALL:	STAT(rx_elem_toosmall);
543 	case S_RX_ELEM_UNKNOWN:	STAT(rx_elem_unknown);
544 	case S_RX_BADCHAN:	STAT(rx_badchan);
545 	case S_RX_CHANMISMATCH:	STAT(rx_chanmismatch);
546 	case S_RX_NODEALLOC:	STAT(rx_nodealloc);
547 	case S_RX_SSIDMISMATCH:	STAT(rx_ssidmismatch);
548 	case S_RX_AUTH_UNSUPPORTED:	STAT(rx_auth_unsupported);
549 	case S_RX_AUTH_FAIL:	STAT(rx_auth_fail);
550 	case S_RX_AUTH_COUNTERMEASURES:	STAT(rx_auth_countermeasures);
551 	case S_RX_ASSOC_BSS:	STAT(rx_assoc_bss);
552 	case S_RX_ASSOC_NOTAUTH:	STAT(rx_assoc_notauth);
553 	case S_RX_ASSOC_CAPMISMATCH:	STAT(rx_assoc_capmismatch);
554 	case S_RX_ASSOC_NORATE:	STAT(rx_assoc_norate);
555 	case S_RX_ASSOC_BADWPAIE:	STAT(rx_assoc_badwpaie);
556 	case S_RX_DEAUTH:	STAT(rx_deauth);
557 	case S_RX_DISASSOC:	STAT(rx_disassoc);
558 	case S_RX_BADSUBTYPE:	STAT(rx_badsubtype);
559 	case S_RX_NOBUF:	STAT(rx_nobuf);
560 	case S_RX_DECRYPTCRC:	STAT(rx_decryptcrc);
561 	case S_RX_AHDEMO_MGT:	STAT(rx_ahdemo_mgt);
562 	case S_RX_BAD_AUTH:	STAT(rx_bad_auth);
563 	case S_RX_UNAUTH:	STAT(rx_unauth);
564 	case S_RX_BADKEYID:	STAT(rx_badkeyid);
565 	case S_RX_CCMPREPLAY:	STAT(rx_ccmpreplay);
566 	case S_RX_CCMPFORMAT:	STAT(rx_ccmpformat);
567 	case S_RX_CCMPMIC:	STAT(rx_ccmpmic);
568 	case S_RX_TKIPREPLAY:	STAT(rx_tkipreplay);
569 	case S_RX_TKIPFORMAT:	STAT(rx_tkipformat);
570 	case S_RX_TKIPMIC:	STAT(rx_tkipmic);
571 	case S_RX_TKIPICV:	STAT(rx_tkipicv);
572 	case S_RX_BADCIPHER:	STAT(rx_badcipher);
573 	case S_RX_NOCIPHERCTX:	STAT(rx_nocipherctx);
574 	case S_RX_ACL:		STAT(rx_acl);
575 	case S_TX_NOBUF:	STAT(tx_nobuf);
576 	case S_TX_NONODE:	STAT(tx_nonode);
577 	case S_TX_UNKNOWNMGT:	STAT(tx_unknownmgt);
578 	case S_TX_BADCIPHER:	STAT(tx_badcipher);
579 	case S_TX_NODEFKEY:	STAT(tx_nodefkey);
580 	case S_TX_NOHEADROOM:	STAT(tx_noheadroom);
581 	case S_TX_FRAGFRAMES:	STAT(tx_fragframes);
582 	case S_TX_FRAGS:	STAT(tx_frags);
583 	case S_SCAN_ACTIVE:	STAT(scan_active);
584 	case S_SCAN_PASSIVE:	STAT(scan_passive);
585 	case S_NODE_TIMEOUT:	STAT(node_timeout);
586 	case S_CRYPTO_NOMEM:	STAT(crypto_nomem);
587 	case S_CRYPTO_TKIP:	STAT(crypto_tkip);
588 	case S_CRYPTO_TKIPENMIC:	STAT(crypto_tkipenmic);
589 	case S_CRYPTO_TKIPDEMIC:	STAT(crypto_tkipdemic);
590 	case S_CRYPTO_TKIPCM:	STAT(crypto_tkipcm);
591 	case S_CRYPTO_CCMP:	STAT(crypto_ccmp);
592 	case S_CRYPTO_WEP:	STAT(crypto_wep);
593 	case S_CRYPTO_SETKEY_CIPHER:	STAT(crypto_setkey_cipher);
594 	case S_CRYPTO_SETKEY_NOKEY:	STAT(crypto_setkey_nokey);
595 	case S_CRYPTO_DELKEY:	STAT(crypto_delkey);
596 	case S_CRYPTO_BADCIPHER:	STAT(crypto_badcipher);
597 	case S_CRYPTO_NOCIPHER:	STAT(crypto_nocipher);
598 	case S_CRYPTO_ATTACHFAIL:	STAT(crypto_attachfail);
599 	case S_CRYPTO_SWFALLBACK:	STAT(crypto_swfallback);
600 	case S_CRYPTO_KEYFAIL:	STAT(crypto_keyfail);
601 	case S_CRYPTO_ENMICFAIL:	STAT(crypto_enmicfail);
602 	case S_IBSS_CAPMISMATCH:	STAT(ibss_capmismatch);
603 	case S_IBSS_NORATE:	STAT(ibss_norate);
604 	case S_PS_UNASSOC:	STAT(ps_unassoc);
605 	case S_PS_BADAID:	STAT(ps_badaid);
606 	case S_PS_QEMPTY:	STAT(ps_qempty);
607 	case S_FF_BADHDR:	STAT(ff_badhdr);
608 	case S_FF_TOOSHORT:	STAT(ff_tooshort);
609 	case S_FF_SPLIT:	STAT(ff_split);
610 	case S_FF_DECAP:	STAT(ff_decap);
611 	case S_FF_ENCAP:	STAT(ff_encap);
612 	case S_RX_BADBINTVAL:	STAT(rx_badbintval);
613 	case S_RX_MGMT:		STAT(rx_mgmt);
614 	case S_RX_DEMICFAIL:	STAT(rx_demicfail);
615 	case S_RX_DEFRAG:	STAT(rx_defrag);
616 	case S_RX_ACTION:	STAT(rx_action);
617 	case S_AMSDU_TOOSHORT:	STAT(amsdu_tooshort);
618 	case S_AMSDU_SPLIT:	STAT(amsdu_split);
619 	case S_AMSDU_DECAP:	STAT(amsdu_decap);
620 	case S_AMSDU_ENCAP:	STAT(amsdu_encap);
621 	case S_AMPDU_REORDER:	STAT(ampdu_rx_reorder);
622 	case S_AMPDU_FLUSH:	STAT(ampdu_rx_flush);
623 	case S_AMPDU_BARBAD:	STAT(ampdu_bar_bad);
624 	case S_AMPDU_BAROOW:	STAT(ampdu_bar_oow);
625 	case S_AMPDU_BARMOVE:	STAT(ampdu_bar_move);
626 	case S_AMPDU_BAR:	STAT(ampdu_bar_rx);
627 	case S_AMPDU_MOVE:	STAT(ampdu_rx_move);
628 	case S_AMPDU_OOR:	STAT(ampdu_rx_oor);
629 	case S_AMPDU_COPY:	STAT(ampdu_rx_copy);
630 	case S_AMPDU_DROP:	STAT(ampdu_rx_drop);
631 	case S_AMPDU_AGE:	STAT(ampdu_rx_age);
632 	case S_AMPDU_STOP:	STAT(ampdu_stop);
633 	case S_AMPDU_STOP_FAILED:STAT(ampdu_stop_failed);
634 	case S_ADDBA_REJECT:	STAT(addba_reject);
635 	case S_ADDBA_NOREQUEST:	STAT(addba_norequest);
636 	case S_ADDBA_BADTOKEN:	STAT(addba_badtoken);
637 	case S_TX_BADSTATE:	STAT(tx_badstate);
638 	case S_TX_NOTASSOC:	STAT(tx_notassoc);
639 	case S_TX_CLASSIFY:	STAT(tx_classify);
640 	case S_HT_ASSOC_NOHTCAP:STAT(ht_assoc_nohtcap);
641 	case S_HT_ASSOC_DOWNGRADE:STAT(ht_assoc_downgrade);
642 	case S_HT_ASSOC_NORATE:	STAT(ht_assoc_norate);
643 	case S_INPUT:		NSTAT(rx_data);
644 	case S_OUTPUT:		NSTAT(tx_data);
645 	case S_RX_UCAST:	NSTAT(rx_ucast);
646 	case S_RX_MCAST:	NSTAT(rx_mcast);
647 	case S_TX_UCAST:	NSTAT(tx_ucast);
648 	case S_TX_MCAST:	NSTAT(tx_mcast);
649 	}
650 	return wlan_getinfo(wf, s, b, bs);
651 #undef NSTAT
652 #undef STAT
653 }
654 
655 static int
656 wlan_get_totstat(struct statfoo *sf, int s, char b[], size_t bs)
657 {
658 	struct wlanstatfoo_p *wf = (struct wlanstatfoo_p *) sf;
659 #define	STAT(x) \
660 	snprintf(b, bs, "%u", wf->total.is_##x); return 1
661 #define	NSTAT(x) \
662 	snprintf(b, bs, "%u", wf->ntotal.is_stats.ns_##x); return 1
663 
664 	switch (s) {
665 	case S_RX_BADVERSION:	STAT(rx_badversion);
666 	case S_RX_TOOSHORT:	STAT(rx_tooshort);
667 	case S_RX_WRONGBSS:	STAT(rx_wrongbss);
668 	case S_RX_DUP:	STAT(rx_dup);
669 	case S_RX_WRONGDIR:	STAT(rx_wrongdir);
670 	case S_RX_MCASTECHO:	STAT(rx_mcastecho);
671 	case S_RX_NOTASSOC:	STAT(rx_notassoc);
672 	case S_RX_NOPRIVACY:	STAT(rx_noprivacy);
673 	case S_RX_UNENCRYPTED:	STAT(rx_unencrypted);
674 	case S_RX_WEPFAIL:	STAT(rx_wepfail);
675 	case S_RX_DECAP:	STAT(rx_decap);
676 	case S_RX_MGTDISCARD:	STAT(rx_mgtdiscard);
677 	case S_RX_CTL:		STAT(rx_ctl);
678 	case S_RX_BEACON:	STAT(rx_beacon);
679 	case S_RX_RSTOOBIG:	STAT(rx_rstoobig);
680 	case S_RX_ELEM_MISSING:	STAT(rx_elem_missing);
681 	case S_RX_ELEM_TOOBIG:	STAT(rx_elem_toobig);
682 	case S_RX_ELEM_TOOSMALL:	STAT(rx_elem_toosmall);
683 	case S_RX_ELEM_UNKNOWN:	STAT(rx_elem_unknown);
684 	case S_RX_BADCHAN:	STAT(rx_badchan);
685 	case S_RX_CHANMISMATCH:	STAT(rx_chanmismatch);
686 	case S_RX_NODEALLOC:	STAT(rx_nodealloc);
687 	case S_RX_SSIDMISMATCH:	STAT(rx_ssidmismatch);
688 	case S_RX_AUTH_UNSUPPORTED:	STAT(rx_auth_unsupported);
689 	case S_RX_AUTH_FAIL:	STAT(rx_auth_fail);
690 	case S_RX_AUTH_COUNTERMEASURES:	STAT(rx_auth_countermeasures);
691 	case S_RX_ASSOC_BSS:	STAT(rx_assoc_bss);
692 	case S_RX_ASSOC_NOTAUTH:	STAT(rx_assoc_notauth);
693 	case S_RX_ASSOC_CAPMISMATCH:	STAT(rx_assoc_capmismatch);
694 	case S_RX_ASSOC_NORATE:	STAT(rx_assoc_norate);
695 	case S_RX_ASSOC_BADWPAIE:	STAT(rx_assoc_badwpaie);
696 	case S_RX_DEAUTH:	STAT(rx_deauth);
697 	case S_RX_DISASSOC:	STAT(rx_disassoc);
698 	case S_RX_BADSUBTYPE:	STAT(rx_badsubtype);
699 	case S_RX_NOBUF:	STAT(rx_nobuf);
700 	case S_RX_DECRYPTCRC:	STAT(rx_decryptcrc);
701 	case S_RX_AHDEMO_MGT:	STAT(rx_ahdemo_mgt);
702 	case S_RX_BAD_AUTH:	STAT(rx_bad_auth);
703 	case S_RX_UNAUTH:	STAT(rx_unauth);
704 	case S_RX_BADKEYID:	STAT(rx_badkeyid);
705 	case S_RX_CCMPREPLAY:	STAT(rx_ccmpreplay);
706 	case S_RX_CCMPFORMAT:	STAT(rx_ccmpformat);
707 	case S_RX_CCMPMIC:	STAT(rx_ccmpmic);
708 	case S_RX_TKIPREPLAY:	STAT(rx_tkipreplay);
709 	case S_RX_TKIPFORMAT:	STAT(rx_tkipformat);
710 	case S_RX_TKIPMIC:	STAT(rx_tkipmic);
711 	case S_RX_TKIPICV:	STAT(rx_tkipicv);
712 	case S_RX_BADCIPHER:	STAT(rx_badcipher);
713 	case S_RX_NOCIPHERCTX:	STAT(rx_nocipherctx);
714 	case S_RX_ACL:		STAT(rx_acl);
715 	case S_TX_NOBUF:	STAT(tx_nobuf);
716 	case S_TX_NONODE:	STAT(tx_nonode);
717 	case S_TX_UNKNOWNMGT:	STAT(tx_unknownmgt);
718 	case S_TX_BADCIPHER:	STAT(tx_badcipher);
719 	case S_TX_NODEFKEY:	STAT(tx_nodefkey);
720 	case S_TX_NOHEADROOM:	STAT(tx_noheadroom);
721 	case S_TX_FRAGFRAMES:	STAT(tx_fragframes);
722 	case S_TX_FRAGS:	STAT(tx_frags);
723 	case S_SCAN_ACTIVE:	STAT(scan_active);
724 	case S_SCAN_PASSIVE:	STAT(scan_passive);
725 	case S_NODE_TIMEOUT:	STAT(node_timeout);
726 	case S_CRYPTO_NOMEM:	STAT(crypto_nomem);
727 	case S_CRYPTO_TKIP:	STAT(crypto_tkip);
728 	case S_CRYPTO_TKIPENMIC:	STAT(crypto_tkipenmic);
729 	case S_CRYPTO_TKIPDEMIC:	STAT(crypto_tkipdemic);
730 	case S_CRYPTO_TKIPCM:	STAT(crypto_tkipcm);
731 	case S_CRYPTO_CCMP:	STAT(crypto_ccmp);
732 	case S_CRYPTO_WEP:	STAT(crypto_wep);
733 	case S_CRYPTO_SETKEY_CIPHER:	STAT(crypto_setkey_cipher);
734 	case S_CRYPTO_SETKEY_NOKEY:	STAT(crypto_setkey_nokey);
735 	case S_CRYPTO_DELKEY:	STAT(crypto_delkey);
736 	case S_CRYPTO_BADCIPHER:	STAT(crypto_badcipher);
737 	case S_CRYPTO_NOCIPHER:	STAT(crypto_nocipher);
738 	case S_CRYPTO_ATTACHFAIL:	STAT(crypto_attachfail);
739 	case S_CRYPTO_SWFALLBACK:	STAT(crypto_swfallback);
740 	case S_CRYPTO_KEYFAIL:	STAT(crypto_keyfail);
741 	case S_CRYPTO_ENMICFAIL:	STAT(crypto_enmicfail);
742 	case S_IBSS_CAPMISMATCH:	STAT(ibss_capmismatch);
743 	case S_IBSS_NORATE:	STAT(ibss_norate);
744 	case S_PS_UNASSOC:	STAT(ps_unassoc);
745 	case S_PS_BADAID:	STAT(ps_badaid);
746 	case S_PS_QEMPTY:	STAT(ps_qempty);
747 	case S_FF_BADHDR:	STAT(ff_badhdr);
748 	case S_FF_TOOSHORT:	STAT(ff_tooshort);
749 	case S_FF_SPLIT:	STAT(ff_split);
750 	case S_FF_DECAP:	STAT(ff_decap);
751 	case S_FF_ENCAP:	STAT(ff_encap);
752 	case S_RX_BADBINTVAL:	STAT(rx_badbintval);
753 	case S_RX_MGMT:		STAT(rx_mgmt);
754 	case S_RX_DEMICFAIL:	STAT(rx_demicfail);
755 	case S_RX_DEFRAG:	STAT(rx_defrag);
756 	case S_RX_ACTION:	STAT(rx_action);
757 	case S_AMSDU_TOOSHORT:	STAT(amsdu_tooshort);
758 	case S_AMSDU_SPLIT:	STAT(amsdu_split);
759 	case S_AMSDU_DECAP:	STAT(amsdu_decap);
760 	case S_AMSDU_ENCAP:	STAT(amsdu_encap);
761 	case S_AMPDU_REORDER:	STAT(ampdu_rx_reorder);
762 	case S_AMPDU_FLUSH:	STAT(ampdu_rx_flush);
763 	case S_AMPDU_BARBAD:	STAT(ampdu_bar_bad);
764 	case S_AMPDU_BAROOW:	STAT(ampdu_bar_oow);
765 	case S_AMPDU_BARMOVE:	STAT(ampdu_bar_move);
766 	case S_AMPDU_BAR:	STAT(ampdu_bar_rx);
767 	case S_AMPDU_MOVE:	STAT(ampdu_rx_move);
768 	case S_AMPDU_OOR:	STAT(ampdu_rx_oor);
769 	case S_AMPDU_COPY:	STAT(ampdu_rx_copy);
770 	case S_AMPDU_DROP:	STAT(ampdu_rx_drop);
771 	case S_AMPDU_AGE:	STAT(ampdu_rx_age);
772 	case S_AMPDU_STOP:	STAT(ampdu_stop);
773 	case S_AMPDU_STOP_FAILED:STAT(ampdu_stop_failed);
774 	case S_ADDBA_REJECT:	STAT(addba_reject);
775 	case S_ADDBA_NOREQUEST:	STAT(addba_norequest);
776 	case S_ADDBA_BADTOKEN:	STAT(addba_badtoken);
777 	case S_TX_BADSTATE:	STAT(tx_badstate);
778 	case S_TX_NOTASSOC:	STAT(tx_notassoc);
779 	case S_TX_CLASSIFY:	STAT(tx_classify);
780 	case S_HT_ASSOC_NOHTCAP:STAT(ht_assoc_nohtcap);
781 	case S_HT_ASSOC_DOWNGRADE:STAT(ht_assoc_downgrade);
782 	case S_HT_ASSOC_NORATE:	STAT(ht_assoc_norate);
783 	case S_INPUT:		NSTAT(rx_data);
784 	case S_OUTPUT:		NSTAT(tx_data);
785 	case S_RX_UCAST:	NSTAT(rx_ucast);
786 	case S_RX_MCAST:	NSTAT(rx_mcast);
787 	case S_TX_UCAST:	NSTAT(tx_ucast);
788 	case S_TX_MCAST:	NSTAT(tx_mcast);
789 	}
790 	return wlan_getinfo(wf, s, b, bs);
791 #undef NSTAT
792 #undef STAT
793 }
794 
795 STATFOO_DEFINE_BOUNCE(wlanstatfoo)
796 
797 struct wlanstatfoo *
798 wlanstats_new(const char *ifname, const char *fmtstring)
799 {
800 #define	N(a)	(sizeof(a) / sizeof(a[0]))
801 	struct wlanstatfoo_p *wf;
802 
803 	wf = calloc(1, sizeof(struct wlanstatfoo_p));
804 	if (wf != NULL) {
805 		statfoo_init(&wf->base.base, "wlanstats", wlanstats, N(wlanstats));
806 		/* override base methods */
807 		wf->base.base.collect_cur = wlan_collect_cur;
808 		wf->base.base.collect_tot = wlan_collect_tot;
809 		wf->base.base.get_curstat = wlan_get_curstat;
810 		wf->base.base.get_totstat = wlan_get_totstat;
811 		wf->base.base.update_tot = wlan_update_tot;
812 
813 		/* setup bounce functions for public methods */
814 		STATFOO_BOUNCE(wf, wlanstatfoo);
815 
816 		/* setup our public methods */
817 		wf->base.setifname = wlan_setifname;
818 		wf->base.getifname = wlan_getifname;
819 		wf->base.getopmode = wlan_getopmode;
820 		wf->base.setstamac = wlan_setstamac;
821 		wf->opmode = -1;
822 
823 		wf->s = socket(AF_INET, SOCK_DGRAM, 0);
824 		if (wf->s < 0)
825 			err(1, "socket");
826 
827 		wlan_setifname(&wf->base, ifname);
828 		wf->base.setfmt(&wf->base, fmtstring);
829 	}
830 	return &wf->base;
831 #undef N
832 }
833