wlandebug.c revision 178359
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 178359 2008-04-20 20:41:47Z sam $ 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 152153317Ssam fprintf(stderr, "usage: %s [-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__ 163178359Ssam snprintf(oid, oidlen, "net.%s.debug", wlan); 164178359Ssam#elif __FreeBSD__ 165178359Ssam snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4); 166178359Ssam#elif __NetBSD__ 167178359Ssam snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan+4); 168178359Ssam#else 169178359Ssam#error "No support for this system" 170178359Ssam#endif 171178359Ssam} 172178359Ssam 173153317Ssamint 174153317Ssammain(int argc, char *argv[]) 175153317Ssam{ 176153317Ssam const char *cp, *tp; 177153317Ssam const char *sep; 178170534Ssam int op, i, unit; 179153317Ssam u_int32_t debug, ndebug; 180153317Ssam size_t debuglen, parentlen; 181178359Ssam char oid[256]; 182153317Ssam 183153317Ssam progname = argv[0]; 184178359Ssam setoid(oid, sizeof(oid), "wlan0"); 185153317Ssam if (argc > 1) { 186153317Ssam if (strcmp(argv[1], "-i") == 0) { 187153317Ssam if (argc < 2) 188153317Ssam errx(1, "missing interface name for -i option"); 189178359Ssam if (strncmp(argv[2], "wlan", 4) != 0) 190178359Ssam errx(1, "expecting a wlan interface name"); 191178359Ssam setoid(oid, sizeof(oid), argv[2]); 192153317Ssam argc -= 2, argv += 2; 193153317Ssam } else if (strcmp(argv[1], "-?") == 0) 194153317Ssam usage(); 195153317Ssam } 196153317Ssam 197153317Ssam debuglen = sizeof(debug); 198153317Ssam if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0) 199153317Ssam err(1, "sysctl-get(%s)", oid); 200153317Ssam ndebug = debug; 201153317Ssam for (; argc > 1; argc--, argv++) { 202153317Ssam cp = argv[1]; 203153317Ssam do { 204153317Ssam u_int bit; 205153317Ssam 206153317Ssam if (*cp == '-') { 207153317Ssam cp++; 208153317Ssam op = -1; 209153317Ssam } else if (*cp == '+') { 210153317Ssam cp++; 211153317Ssam op = 1; 212153317Ssam } else 213153317Ssam op = 0; 214153317Ssam for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';) 215153317Ssam tp++; 216153317Ssam bit = getflag(cp, tp-cp); 217153317Ssam if (op < 0) 218153317Ssam ndebug &= ~bit; 219153317Ssam else if (op > 0) 220153317Ssam ndebug |= bit; 221153317Ssam else { 222153317Ssam if (bit == 0) { 223170534Ssam int c = *cp; 224170534Ssam if (isdigit(c)) 225153317Ssam bit = strtoul(cp, NULL, 0); 226153317Ssam else 227153317Ssam errx(1, "unknown flag %.*s", 228170534Ssam (int)(tp-cp), cp); 229153317Ssam } 230153317Ssam ndebug = bit; 231153317Ssam } 232153317Ssam } while (*(cp = tp) != '\0'); 233153317Ssam } 234153317Ssam if (debug != ndebug) { 235153317Ssam printf("%s: 0x%x => ", oid, debug); 236153317Ssam if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0) 237153317Ssam err(1, "sysctl-set(%s)", oid); 238153317Ssam printf("0x%x", ndebug); 239153317Ssam debug = ndebug; 240153317Ssam } else 241153317Ssam printf("%s: 0x%x", oid, debug); 242153317Ssam sep = "<"; 243153317Ssam for (i = 0; i < N(flags); i++) 244153317Ssam if (debug & flags[i].bit) { 245153317Ssam printf("%s%s", sep, flags[i].name); 246153317Ssam sep = ","; 247153317Ssam } 248153317Ssam printf("%s\n", *sep != '<' ? ">" : ""); 249153317Ssam return 0; 250153317Ssam} 251