wlandebug.c revision 179397
1/*- 2 * Copyright (c) 2002-2008 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: head/usr.sbin/wlandebug/wlandebug.c 179397 2008-05-28 23:36:47Z sam $ 30 */ 31 32/* 33 * wlandebug [-i interface] flags 34 * (default interface is wlan.0). 35 */ 36#include <sys/types.h> 37 38#include <stdio.h> 39#include <stdlib.h> 40#include <ctype.h> 41#include <getopt.h> 42#include <string.h> 43#include <err.h> 44 45#define N(a) (sizeof(a)/sizeof(a[0])) 46 47const char *progname; 48 49#define IEEE80211_MSG_11N 0x80000000 /* 11n mode debug */ 50#define IEEE80211_MSG_DEBUG 0x40000000 /* IFF_DEBUG equivalent */ 51#define IEEE80211_MSG_DUMPPKTS 0x20000000 /* IFF_LINK2 equivalant */ 52#define IEEE80211_MSG_CRYPTO 0x10000000 /* crypto work */ 53#define IEEE80211_MSG_INPUT 0x08000000 /* input handling */ 54#define IEEE80211_MSG_XRATE 0x04000000 /* rate set handling */ 55#define IEEE80211_MSG_ELEMID 0x02000000 /* element id parsing */ 56#define IEEE80211_MSG_NODE 0x01000000 /* node handling */ 57#define IEEE80211_MSG_ASSOC 0x00800000 /* association handling */ 58#define IEEE80211_MSG_AUTH 0x00400000 /* authentication handling */ 59#define IEEE80211_MSG_SCAN 0x00200000 /* scanning */ 60#define IEEE80211_MSG_OUTPUT 0x00100000 /* output handling */ 61#define IEEE80211_MSG_STATE 0x00080000 /* state machine */ 62#define IEEE80211_MSG_POWER 0x00040000 /* power save handling */ 63#define IEEE80211_MSG_DOT1X 0x00020000 /* 802.1x authenticator */ 64#define IEEE80211_MSG_DOT1XSM 0x00010000 /* 802.1x state machine */ 65#define IEEE80211_MSG_RADIUS 0x00008000 /* 802.1x radius client */ 66#define IEEE80211_MSG_RADDUMP 0x00004000 /* dump 802.1x radius packets */ 67#define IEEE80211_MSG_RADKEYS 0x00002000 /* dump 802.1x keys */ 68#define IEEE80211_MSG_WPA 0x00001000 /* WPA/RSN protocol */ 69#define IEEE80211_MSG_ACL 0x00000800 /* ACL handling */ 70#define IEEE80211_MSG_WME 0x00000400 /* WME protocol */ 71#define IEEE80211_MSG_SUPERG 0x00000200 /* Atheros SuperG protocol */ 72#define IEEE80211_MSG_DOTH 0x00000100 /* 802.11h support */ 73#define IEEE80211_MSG_INACT 0x00000080 /* inactivity handling */ 74#define IEEE80211_MSG_ROAM 0x00000040 /* sta-mode roaming */ 75#define IEEE80211_MSG_RATECTL 0x00000020 /* tx rate control */ 76#define IEEE80211_MSG_ACTION 0x00000010 /* action frame handling */ 77#define IEEE80211_MSG_WDS 0x00000008 /* WDS handling */ 78#define IEEE80211_MSG_IOCTL 0x00000004 /* ioctl handling */ 79#define IEEE80211_MSG_ADDBA 0x00000002 /* ADDBA handling */ 80 81static struct { 82 const char *name; 83 u_int bit; 84} flags[] = { 85 { "11n", IEEE80211_MSG_11N }, 86 { "debug", IEEE80211_MSG_DEBUG }, 87 { "dumppkts", IEEE80211_MSG_DUMPPKTS }, 88 { "crypto", IEEE80211_MSG_CRYPTO }, 89 { "input", IEEE80211_MSG_INPUT }, 90 { "xrate", IEEE80211_MSG_XRATE }, 91 { "elemid", IEEE80211_MSG_ELEMID }, 92 { "node", IEEE80211_MSG_NODE }, 93 { "assoc", IEEE80211_MSG_ASSOC }, 94 { "auth", IEEE80211_MSG_AUTH }, 95 { "scan", IEEE80211_MSG_SCAN }, 96 { "output", IEEE80211_MSG_OUTPUT }, 97 { "state", IEEE80211_MSG_STATE }, 98 { "power", IEEE80211_MSG_POWER }, 99 { "dot1x", IEEE80211_MSG_DOT1X }, 100 { "dot1xsm", IEEE80211_MSG_DOT1XSM }, 101 { "radius", IEEE80211_MSG_RADIUS }, 102 { "raddump", IEEE80211_MSG_RADDUMP }, 103 { "radkeys", IEEE80211_MSG_RADKEYS }, 104 { "wpa", IEEE80211_MSG_WPA }, 105 { "acl", IEEE80211_MSG_ACL }, 106 { "wme", IEEE80211_MSG_WME }, 107 { "superg", IEEE80211_MSG_SUPERG }, 108 { "doth", IEEE80211_MSG_DOTH }, 109 { "inact", IEEE80211_MSG_INACT }, 110 { "roam", IEEE80211_MSG_ROAM }, 111 { "rate", IEEE80211_MSG_RATECTL }, 112 { "action", IEEE80211_MSG_ACTION }, 113 { "wds", IEEE80211_MSG_WDS }, 114 { "ioctl", IEEE80211_MSG_IOCTL }, 115 { "addba", IEEE80211_MSG_ADDBA }, 116}; 117 118static u_int 119getflag(const char *name, int len) 120{ 121 int i; 122 123 for (i = 0; i < N(flags); i++) 124 if (strncasecmp(flags[i].name, name, len) == 0) 125 return flags[i].bit; 126 return 0; 127} 128 129static const char * 130getflagname(u_int flag) 131{ 132 int i; 133 134 for (i = 0; i < N(flags); i++) 135 if (flags[i].bit == flag) 136 return flags[i].name; 137 return "???"; 138} 139 140static void 141usage(void) 142{ 143 int i; 144 145 fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname); 146 fprintf(stderr, "where flags are:\n"); 147 for (i = 0; i < N(flags); i++) 148 printf("%s\n", flags[i].name); 149 exit(-1); 150} 151 152static void 153setoid(char oid[], size_t oidlen, const char *wlan) 154{ 155#ifdef __linux__ 156 if (wlan) 157 snprintf(oid, oidlen, "net.%s.debug", wlan); 158#elif __FreeBSD__ 159 if (wlan) 160 snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4); 161 else 162 snprintf(oid, oidlen, "net.wlan.debug"); 163#elif __NetBSD__ 164 if (wlan) 165 snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan+4); 166 else 167 snprintf(oid, oidlen, "net.link.ieee80211.debug"); 168#else 169#error "No support for this system" 170#endif 171} 172 173int 174main(int argc, char *argv[]) 175{ 176 const char *cp, *tp; 177 const char *sep; 178 int op, i, unit; 179 u_int32_t debug, ndebug; 180 size_t debuglen, parentlen; 181 char oid[256]; 182 183 progname = argv[0]; 184 setoid(oid, sizeof(oid), "wlan0"); 185 if (argc > 1) { 186 if (strcmp(argv[1], "-d") == 0) { 187 setoid(oid, sizeof(oid), NULL); 188 argc -= 1, argv += 1; 189 } else if (strcmp(argv[1], "-i") == 0) { 190 if (argc < 2) 191 errx(1, "missing interface name for -i option"); 192 if (strncmp(argv[2], "wlan", 4) != 0) 193 errx(1, "expecting a wlan interface name"); 194 setoid(oid, sizeof(oid), argv[2]); 195 argc -= 2, argv += 2; 196 } else if (strcmp(argv[1], "-?") == 0) 197 usage(); 198 } 199 200 debuglen = sizeof(debug); 201 if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0) 202 err(1, "sysctl-get(%s)", oid); 203 ndebug = debug; 204 for (; argc > 1; argc--, argv++) { 205 cp = argv[1]; 206 do { 207 u_int bit; 208 209 if (*cp == '-') { 210 cp++; 211 op = -1; 212 } else if (*cp == '+') { 213 cp++; 214 op = 1; 215 } else 216 op = 0; 217 for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';) 218 tp++; 219 bit = getflag(cp, tp-cp); 220 if (op < 0) 221 ndebug &= ~bit; 222 else if (op > 0) 223 ndebug |= bit; 224 else { 225 if (bit == 0) { 226 int c = *cp; 227 if (isdigit(c)) 228 bit = strtoul(cp, NULL, 0); 229 else 230 errx(1, "unknown flag %.*s", 231 (int)(tp-cp), cp); 232 } 233 ndebug = bit; 234 } 235 } while (*(cp = tp) != '\0'); 236 } 237 if (debug != ndebug) { 238 printf("%s: 0x%x => ", oid, debug); 239 if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0) 240 err(1, "sysctl-set(%s)", oid); 241 printf("0x%x", ndebug); 242 debug = ndebug; 243 } else 244 printf("%s: 0x%x", oid, debug); 245 sep = "<"; 246 for (i = 0; i < N(flags); i++) 247 if (debug & flags[i].bit) { 248 printf("%s%s", sep, flags[i].name); 249 sep = ","; 250 } 251 printf("%s\n", *sep != '<' ? ">" : ""); 252 return 0; 253} 254