wlandebug.c revision 179121
1153317Ssam/*- 2153317Ssam * Copyright (c) 2002-2004 Sam Leffler, Errno Consulting 3153317Ssam * All rights reserved. 4153317Ssam * 5153317Ssam * Redistribution and use in source and binary forms, with or without 6153317Ssam * modification, are permitted provided that the following conditions 7153317Ssam * are met: 8153317Ssam * 1. Redistributions of source code must retain the above copyright 9153317Ssam * notice, this list of conditions and the following disclaimer, 10153317Ssam * without modification. 11153317Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12153317Ssam * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13153317Ssam * redistribution must be conditioned upon including a substantially 14153317Ssam * similar Disclaimer requirement for further binary redistribution. 15153317Ssam * 3. Neither the names of the above-listed copyright holders nor the names 16153317Ssam * of any contributors may be used to endorse or promote products derived 17153317Ssam * from this software without specific prior written permission. 18153317Ssam * 19153317Ssam * Alternatively, this software may be distributed under the terms of the 20153317Ssam * GNU General Public License ("GPL") version 2 as published by the Free 21153317Ssam * Software Foundation. 22153317Ssam * 23153317Ssam * NO WARRANTY 24153317Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25153317Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26153317Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 27153317Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 28153317Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 29153317Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30153317Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31153317Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 32153317Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33153317Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 34153317Ssam * THE POSSIBILITY OF SUCH DAMAGES. 35153317Ssam * 36153317Ssam * $FreeBSD: head/usr.sbin/wlandebug/wlandebug.c 179121 2008-05-19 17:49:57Z thompsa $ 37153317Ssam */ 38153317Ssam 39153317Ssam/* 40153317Ssam * wlandebug [-i interface] flags 41153317Ssam * (default interface is wlan.0). 42153317Ssam */ 43153317Ssam#include <sys/types.h> 44153317Ssam 45153317Ssam#include <stdio.h> 46170534Ssam#include <stdlib.h> 47153317Ssam#include <ctype.h> 48153317Ssam#include <getopt.h> 49153317Ssam#include <string.h> 50170534Ssam#include <err.h> 51153317Ssam 52153317Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 53153317Ssam 54153317Ssamconst char *progname; 55153317Ssam 56170534Ssam#define IEEE80211_MSG_11N 0x80000000 /* 11n mode debug */ 57153317Ssam#define IEEE80211_MSG_DEBUG 0x40000000 /* IFF_DEBUG equivalent */ 58153317Ssam#define IEEE80211_MSG_DUMPPKTS 0x20000000 /* IFF_LINK2 equivalant */ 59153317Ssam#define IEEE80211_MSG_CRYPTO 0x10000000 /* crypto work */ 60153317Ssam#define IEEE80211_MSG_INPUT 0x08000000 /* input handling */ 61153317Ssam#define IEEE80211_MSG_XRATE 0x04000000 /* rate set handling */ 62153317Ssam#define IEEE80211_MSG_ELEMID 0x02000000 /* element id parsing */ 63153317Ssam#define IEEE80211_MSG_NODE 0x01000000 /* node handling */ 64153317Ssam#define IEEE80211_MSG_ASSOC 0x00800000 /* association handling */ 65153317Ssam#define IEEE80211_MSG_AUTH 0x00400000 /* authentication handling */ 66153317Ssam#define IEEE80211_MSG_SCAN 0x00200000 /* scanning */ 67153317Ssam#define IEEE80211_MSG_OUTPUT 0x00100000 /* output handling */ 68153317Ssam#define IEEE80211_MSG_STATE 0x00080000 /* state machine */ 69153317Ssam#define IEEE80211_MSG_POWER 0x00040000 /* power save handling */ 70153317Ssam#define IEEE80211_MSG_DOT1X 0x00020000 /* 802.1x authenticator */ 71153317Ssam#define IEEE80211_MSG_DOT1XSM 0x00010000 /* 802.1x state machine */ 72153317Ssam#define IEEE80211_MSG_RADIUS 0x00008000 /* 802.1x radius client */ 73153317Ssam#define IEEE80211_MSG_RADDUMP 0x00004000 /* dump 802.1x radius packets */ 74153317Ssam#define IEEE80211_MSG_RADKEYS 0x00002000 /* dump 802.1x keys */ 75153317Ssam#define IEEE80211_MSG_WPA 0x00001000 /* WPA/RSN protocol */ 76153317Ssam#define IEEE80211_MSG_ACL 0x00000800 /* ACL handling */ 77153317Ssam#define IEEE80211_MSG_WME 0x00000400 /* WME protocol */ 78153317Ssam#define IEEE80211_MSG_SUPERG 0x00000200 /* Atheros SuperG protocol */ 79153317Ssam#define IEEE80211_MSG_DOTH 0x00000100 /* 802.11h support */ 80153317Ssam#define IEEE80211_MSG_INACT 0x00000080 /* inactivity handling */ 81153317Ssam#define IEEE80211_MSG_ROAM 0x00000040 /* sta-mode roaming */ 82164635Ssam#define IEEE80211_MSG_RATECTL 0x00000020 /* tx rate control */ 83178359Ssam#define IEEE80211_MSG_ACTION 0x00000010 /* action frame handling */ 84178359Ssam#define IEEE80211_MSG_WDS 0x00000008 /* WDS handling */ 85178359Ssam#define IEEE80211_MSG_IOCTL 0x00000004 /* ioctl handling */ 86178359Ssam#define IEEE80211_MSG_ADDBA 0x00000002 /* ADDBA handling */ 87153317Ssam 88153317Ssamstatic struct { 89153317Ssam const char *name; 90153317Ssam u_int bit; 91153317Ssam} flags[] = { 92170534Ssam { "11n", IEEE80211_MSG_11N }, 93153317Ssam { "debug", IEEE80211_MSG_DEBUG }, 94153317Ssam { "dumppkts", IEEE80211_MSG_DUMPPKTS }, 95153317Ssam { "crypto", IEEE80211_MSG_CRYPTO }, 96153317Ssam { "input", IEEE80211_MSG_INPUT }, 97153317Ssam { "xrate", IEEE80211_MSG_XRATE }, 98153317Ssam { "elemid", IEEE80211_MSG_ELEMID }, 99153317Ssam { "node", IEEE80211_MSG_NODE }, 100153317Ssam { "assoc", IEEE80211_MSG_ASSOC }, 101153317Ssam { "auth", IEEE80211_MSG_AUTH }, 102153317Ssam { "scan", IEEE80211_MSG_SCAN }, 103153317Ssam { "output", IEEE80211_MSG_OUTPUT }, 104153317Ssam { "state", IEEE80211_MSG_STATE }, 105153317Ssam { "power", IEEE80211_MSG_POWER }, 106165963Ssam { "dot1x", IEEE80211_MSG_DOT1X }, 107153317Ssam { "dot1xsm", IEEE80211_MSG_DOT1XSM }, 108153317Ssam { "radius", IEEE80211_MSG_RADIUS }, 109153317Ssam { "raddump", IEEE80211_MSG_RADDUMP }, 110153317Ssam { "radkeys", IEEE80211_MSG_RADKEYS }, 111153317Ssam { "wpa", IEEE80211_MSG_WPA }, 112153317Ssam { "acl", IEEE80211_MSG_ACL }, 113153317Ssam { "wme", IEEE80211_MSG_WME }, 114153317Ssam { "superg", IEEE80211_MSG_SUPERG }, 115153317Ssam { "doth", IEEE80211_MSG_DOTH }, 116153317Ssam { "inact", IEEE80211_MSG_INACT }, 117153317Ssam { "roam", IEEE80211_MSG_ROAM }, 118164635Ssam { "rate", IEEE80211_MSG_RATECTL }, 119178359Ssam { "action", IEEE80211_MSG_ACTION }, 120178359Ssam { "wds", IEEE80211_MSG_WDS }, 121178359Ssam { "ioctl", IEEE80211_MSG_IOCTL }, 122178359Ssam { "addba", IEEE80211_MSG_ADDBA }, 123153317Ssam}; 124153317Ssam 125153317Ssamstatic u_int 126153317Ssamgetflag(const char *name, int len) 127153317Ssam{ 128153317Ssam int i; 129153317Ssam 130153317Ssam for (i = 0; i < N(flags); i++) 131153317Ssam if (strncasecmp(flags[i].name, name, len) == 0) 132153317Ssam return flags[i].bit; 133153317Ssam return 0; 134153317Ssam} 135153317Ssam 136153317Ssamstatic const char * 137153317Ssamgetflagname(u_int flag) 138153317Ssam{ 139153317Ssam int i; 140153317Ssam 141153317Ssam for (i = 0; i < N(flags); i++) 142153317Ssam if (flags[i].bit == flag) 143153317Ssam return flags[i].name; 144153317Ssam return "???"; 145153317Ssam} 146153317Ssam 147153317Ssamstatic void 148153317Ssamusage(void) 149153317Ssam{ 150153317Ssam int i; 151153317Ssam 152179121Sthompsa fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname); 153153317Ssam fprintf(stderr, "where flags are:\n"); 154153317Ssam for (i = 0; i < N(flags); i++) 155153317Ssam printf("%s\n", flags[i].name); 156153317Ssam exit(-1); 157153317Ssam} 158153317Ssam 159178359Ssamstatic void 160178359Ssamsetoid(char oid[], size_t oidlen, const char *wlan) 161178359Ssam{ 162178359Ssam#ifdef __linux__ 163179121Sthompsa if (wlan) 164179121Sthompsa snprintf(oid, oidlen, "net.%s.debug", wlan); 165178359Ssam#elif __FreeBSD__ 166179121Sthompsa if (wlan) 167179121Sthompsa snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4); 168179121Sthompsa else 169179121Sthompsa snprintf(oid, oidlen, "net.wlan.debug"); 170178359Ssam#elif __NetBSD__ 171179121Sthompsa if (wlan) 172179121Sthompsa snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan+4); 173179121Sthompsa else 174179121Sthompsa snprintf(oid, oidlen, "net.link.ieee80211.debug"); 175178359Ssam#else 176178359Ssam#error "No support for this system" 177178359Ssam#endif 178178359Ssam} 179178359Ssam 180153317Ssamint 181153317Ssammain(int argc, char *argv[]) 182153317Ssam{ 183153317Ssam const char *cp, *tp; 184153317Ssam const char *sep; 185170534Ssam int op, i, unit; 186153317Ssam u_int32_t debug, ndebug; 187153317Ssam size_t debuglen, parentlen; 188178359Ssam char oid[256]; 189153317Ssam 190153317Ssam progname = argv[0]; 191178359Ssam setoid(oid, sizeof(oid), "wlan0"); 192153317Ssam if (argc > 1) { 193179121Sthompsa if (strcmp(argv[1], "-d") == 0) { 194179121Sthompsa setoid(oid, sizeof(oid), NULL); 195179121Sthompsa argc -= 1, argv += 1; 196179121Sthompsa } else if (strcmp(argv[1], "-i") == 0) { 197153317Ssam if (argc < 2) 198153317Ssam errx(1, "missing interface name for -i option"); 199178359Ssam if (strncmp(argv[2], "wlan", 4) != 0) 200178359Ssam errx(1, "expecting a wlan interface name"); 201178359Ssam setoid(oid, sizeof(oid), argv[2]); 202153317Ssam argc -= 2, argv += 2; 203153317Ssam } else if (strcmp(argv[1], "-?") == 0) 204153317Ssam usage(); 205153317Ssam } 206153317Ssam 207153317Ssam debuglen = sizeof(debug); 208153317Ssam if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0) 209153317Ssam err(1, "sysctl-get(%s)", oid); 210153317Ssam ndebug = debug; 211153317Ssam for (; argc > 1; argc--, argv++) { 212153317Ssam cp = argv[1]; 213153317Ssam do { 214153317Ssam u_int bit; 215153317Ssam 216153317Ssam if (*cp == '-') { 217153317Ssam cp++; 218153317Ssam op = -1; 219153317Ssam } else if (*cp == '+') { 220153317Ssam cp++; 221153317Ssam op = 1; 222153317Ssam } else 223153317Ssam op = 0; 224153317Ssam for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';) 225153317Ssam tp++; 226153317Ssam bit = getflag(cp, tp-cp); 227153317Ssam if (op < 0) 228153317Ssam ndebug &= ~bit; 229153317Ssam else if (op > 0) 230153317Ssam ndebug |= bit; 231153317Ssam else { 232153317Ssam if (bit == 0) { 233170534Ssam int c = *cp; 234170534Ssam if (isdigit(c)) 235153317Ssam bit = strtoul(cp, NULL, 0); 236153317Ssam else 237153317Ssam errx(1, "unknown flag %.*s", 238170534Ssam (int)(tp-cp), cp); 239153317Ssam } 240153317Ssam ndebug = bit; 241153317Ssam } 242153317Ssam } while (*(cp = tp) != '\0'); 243153317Ssam } 244153317Ssam if (debug != ndebug) { 245153317Ssam printf("%s: 0x%x => ", oid, debug); 246153317Ssam if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0) 247153317Ssam err(1, "sysctl-set(%s)", oid); 248153317Ssam printf("0x%x", ndebug); 249153317Ssam debug = ndebug; 250153317Ssam } else 251153317Ssam printf("%s: 0x%x", oid, debug); 252153317Ssam sep = "<"; 253153317Ssam for (i = 0; i < N(flags); i++) 254153317Ssam if (debug & flags[i].bit) { 255153317Ssam printf("%s%s", sep, flags[i].name); 256153317Ssam sep = ","; 257153317Ssam } 258153317Ssam printf("%s\n", *sep != '<' ? ">" : ""); 259153317Ssam return 0; 260153317Ssam} 261