1 /* $NetBSD: ieee80211_20.c,v 1.7 2021/09/07 11:43:02 riastradh Exp $ */ 2 /*- 3 * Copyright (c) 2001 Atsushi Onoe 4 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * Alternatively, this software may be distributed under the terms of the 19 * GNU General Public License ("GPL") version 2 as published by the Free 20 * Software Foundation. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #ifdef __FreeBSD__ 36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_ioctl.c,v 1.35 2005/08/30 14:27:47 avatar Exp $"); 37 #endif 38 #ifdef __NetBSD__ 39 __KERNEL_RCSID(0, "$NetBSD: ieee80211_20.c,v 1.7 2021/09/07 11:43:02 riastradh Exp $"); 40 #endif 41 42 /* 43 * IEEE 802.11 ioctl support 44 */ 45 46 #ifdef _KERNEL_OPT 47 #include "opt_inet.h" 48 #include "opt_compat_netbsd.h" 49 #endif 50 51 #include <sys/endian.h> 52 #include <sys/param.h> 53 #include <sys/kernel.h> 54 #include <sys/socket.h> 55 #include <sys/sockio.h> 56 #include <sys/systm.h> 57 #include <sys/proc.h> 58 #include <sys/kauth.h> 59 #include <sys/compat_stub.h> 60 61 #include <net/if.h> 62 #include <net/if_arp.h> 63 #include <net/if_media.h> 64 #include <net/if_ether.h> 65 66 #include <net80211/ieee80211_var.h> 67 #include <net80211/ieee80211_ioctl.h> 68 69 #include <dev/ic/wi_ieee.h> 70 71 #include <compat/common/compat_mod.h> 72 73 #include <compat/sys/sockio.h> 74 75 static void 76 ieee80211_get_ostats(struct ieee80211_ostats *ostats, 77 struct ieee80211_stats *stats) 78 { 79 80 memset(ostats, 0, sizeof(*ostats)); 81 82 #define COPYSTATS1(__ostats, __nstats, __dstmemb, __srcmemb, __lastmemb)\ 83 (void)memcpy(&(__ostats)->__dstmemb, &(__nstats)->__srcmemb, \ 84 offsetof(struct ieee80211_stats, __lastmemb) - \ 85 offsetof(struct ieee80211_stats, __srcmemb)) 86 #define COPYSTATS(__ostats, __nstats, __dstmemb, __lastmemb) \ 87 COPYSTATS1(__ostats, __nstats, __dstmemb, __dstmemb, __lastmemb) 88 89 COPYSTATS(ostats, stats, is_rx_badversion, is_rx_unencrypted); 90 COPYSTATS(ostats, stats, is_rx_wepfail, is_rx_beacon); 91 COPYSTATS(ostats, stats, is_rx_rstoobig, is_rx_auth_countermeasures); 92 COPYSTATS(ostats, stats, is_rx_assoc_bss, is_rx_assoc_badwpaie); 93 COPYSTATS(ostats, stats, is_rx_deauth, is_rx_unauth); 94 COPYSTATS1(ostats, stats, is_tx_nombuf, is_tx_nobuf, is_tx_badcipher); 95 COPYSTATS(ostats, stats, is_scan_active, is_crypto_tkip); 96 } 97 98 static int 99 ieee80211_20_ioctl(struct ieee80211com *ic, u_long cmd, void *data) 100 { 101 struct ieee80211_ostats ostats; 102 struct ifreq *ifr; 103 int s, error; 104 105 switch (cmd) { 106 case OSIOCG80211STATS: 107 case OSIOCG80211ZSTATS: 108 s = splnet(); 109 ifr = (struct ifreq *)data; 110 ieee80211_get_ostats(&ostats, &ic->ic_stats); 111 error = copyout(&ostats, ifr->ifr_data, sizeof(ostats)); 112 if (error == 0 && cmd == OSIOCG80211ZSTATS) 113 (void)memset(&ic->ic_stats, 0, sizeof(ic->ic_stats)); 114 splx(s); 115 return error; 116 default: 117 return EPASSTHROUGH; 118 } 119 } 120 121 void 122 ieee80211_20_init(void) 123 { 124 125 MODULE_HOOK_SET(ieee80211_ioctl_20_hook, ieee80211_20_ioctl); 126 } 127 128 void 129 ieee80211_20_fini(void) 130 { 131 132 MODULE_HOOK_UNSET(ieee80211_ioctl_20_hook); 133 } 134