xref: /freebsd/usr.sbin/wlandebug/wlandebug.c (revision 7bd6fde3)
1 /*-
2  * Copyright (c) 2002-2004 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  * 3. Neither the names of the above-listed copyright holders nor the names
16  *    of any contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * $FreeBSD$
37  */
38 
39 /*
40  * wlandebug [-i interface] flags
41  * (default interface is wlan.0).
42  */
43 #include <sys/types.h>
44 
45 #include <stdio.h>
46 #include <ctype.h>
47 #include <getopt.h>
48 #include <string.h>
49 
50 #define	N(a)	(sizeof(a)/sizeof(a[0]))
51 
52 const char *progname;
53 
54 #define	IEEE80211_MSG_DEBUG	0x40000000	/* IFF_DEBUG equivalent */
55 #define	IEEE80211_MSG_DUMPPKTS	0x20000000	/* IFF_LINK2 equivalant */
56 #define	IEEE80211_MSG_CRYPTO	0x10000000	/* crypto work */
57 #define	IEEE80211_MSG_INPUT	0x08000000	/* input handling */
58 #define	IEEE80211_MSG_XRATE	0x04000000	/* rate set handling */
59 #define	IEEE80211_MSG_ELEMID	0x02000000	/* element id parsing */
60 #define	IEEE80211_MSG_NODE	0x01000000	/* node handling */
61 #define	IEEE80211_MSG_ASSOC	0x00800000	/* association handling */
62 #define	IEEE80211_MSG_AUTH	0x00400000	/* authentication handling */
63 #define	IEEE80211_MSG_SCAN	0x00200000	/* scanning */
64 #define	IEEE80211_MSG_OUTPUT	0x00100000	/* output handling */
65 #define	IEEE80211_MSG_STATE	0x00080000	/* state machine */
66 #define	IEEE80211_MSG_POWER	0x00040000	/* power save handling */
67 #define	IEEE80211_MSG_DOT1X	0x00020000	/* 802.1x authenticator */
68 #define	IEEE80211_MSG_DOT1XSM	0x00010000	/* 802.1x state machine */
69 #define	IEEE80211_MSG_RADIUS	0x00008000	/* 802.1x radius client */
70 #define	IEEE80211_MSG_RADDUMP	0x00004000	/* dump 802.1x radius packets */
71 #define	IEEE80211_MSG_RADKEYS	0x00002000	/* dump 802.1x keys */
72 #define	IEEE80211_MSG_WPA	0x00001000	/* WPA/RSN protocol */
73 #define	IEEE80211_MSG_ACL	0x00000800	/* ACL handling */
74 #define	IEEE80211_MSG_WME	0x00000400	/* WME protocol */
75 #define	IEEE80211_MSG_SUPERG	0x00000200	/* Atheros SuperG protocol */
76 #define	IEEE80211_MSG_DOTH	0x00000100	/* 802.11h support */
77 #define	IEEE80211_MSG_INACT	0x00000080	/* inactivity handling */
78 #define	IEEE80211_MSG_ROAM	0x00000040	/* sta-mode roaming */
79 #define	IEEE80211_MSG_RATECTL	0x00000020	/* tx rate control */
80 
81 static struct {
82 	const char	*name;
83 	u_int		bit;
84 } flags[] = {
85 	{ "debug",	IEEE80211_MSG_DEBUG },
86 	{ "dumppkts",	IEEE80211_MSG_DUMPPKTS },
87 	{ "crypto",	IEEE80211_MSG_CRYPTO },
88 	{ "input",	IEEE80211_MSG_INPUT },
89 	{ "xrate",	IEEE80211_MSG_XRATE },
90 	{ "elemid",	IEEE80211_MSG_ELEMID },
91 	{ "node",	IEEE80211_MSG_NODE },
92 	{ "assoc",	IEEE80211_MSG_ASSOC },
93 	{ "auth",	IEEE80211_MSG_AUTH },
94 	{ "scan",	IEEE80211_MSG_SCAN },
95 	{ "output",	IEEE80211_MSG_OUTPUT },
96 	{ "state",	IEEE80211_MSG_STATE },
97 	{ "power",	IEEE80211_MSG_POWER },
98 	{ "dot1x",	IEEE80211_MSG_DOT1X },
99 	{ "dot1xsm",	IEEE80211_MSG_DOT1XSM },
100 	{ "radius",	IEEE80211_MSG_RADIUS },
101 	{ "raddump",	IEEE80211_MSG_RADDUMP },
102 	{ "radkeys",	IEEE80211_MSG_RADKEYS },
103 	{ "wpa",	IEEE80211_MSG_WPA },
104 	{ "acl",	IEEE80211_MSG_ACL },
105 	{ "wme",	IEEE80211_MSG_WME },
106 	{ "superg",	IEEE80211_MSG_SUPERG },
107 	{ "doth",	IEEE80211_MSG_DOTH },
108 	{ "inact",	IEEE80211_MSG_INACT },
109 	{ "roam",	IEEE80211_MSG_ROAM },
110 	{ "rate",	IEEE80211_MSG_RATECTL },
111 };
112 
113 static u_int
114 getflag(const char *name, int len)
115 {
116 	int i;
117 
118 	for (i = 0; i < N(flags); i++)
119 		if (strncasecmp(flags[i].name, name, len) == 0)
120 			return flags[i].bit;
121 	return 0;
122 }
123 
124 static const char *
125 getflagname(u_int flag)
126 {
127 	int i;
128 
129 	for (i = 0; i < N(flags); i++)
130 		if (flags[i].bit == flag)
131 			return flags[i].name;
132 	return "???";
133 }
134 
135 static void
136 usage(void)
137 {
138 	int i;
139 
140 	fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
141 	fprintf(stderr, "where flags are:\n");
142 	for (i = 0; i < N(flags); i++)
143 		printf("%s\n", flags[i].name);
144 	exit(-1);
145 }
146 
147 int
148 main(int argc, char *argv[])
149 {
150 	const char *ifname = "ath0";
151 	const char *cp, *tp;
152 	const char *sep;
153 	int c, op, i, unit;
154 	u_int32_t debug, ndebug;
155 	size_t debuglen, parentlen;
156 	char oid[256], parent[256];
157 
158 	progname = argv[0];
159 	if (argc > 1) {
160 		if (strcmp(argv[1], "-i") == 0) {
161 			if (argc < 2)
162 				errx(1, "missing interface name for -i option");
163 			ifname = argv[2];
164 			argc -= 2, argv += 2;
165 		} else if (strcmp(argv[1], "-?") == 0)
166 			usage();
167 	}
168 
169 	for (unit = 0; unit < 10; unit++) {
170 #ifdef __linux__
171 		snprintf(oid, sizeof(oid), "net.wlan%d.%%parent", unit);
172 #else
173 		snprintf(oid, sizeof(oid), "net.wlan.%d.%%parent", unit);
174 #endif
175 		parentlen = sizeof(parent);
176 		if (sysctlbyname(oid, parent, &parentlen, NULL, 0) >= 0 &&
177 		    strncmp(parent, ifname, parentlen) == 0)
178 			break;
179 	}
180 	if (unit == 10)
181 		errx(1, "%s: cannot locate wlan sysctl node.", ifname);
182 #ifdef __linux__
183 	snprintf(oid, sizeof(oid), "net.wlan%d.debug", unit);
184 #else
185 	snprintf(oid, sizeof(oid), "net.wlan.%d.debug", unit);
186 #endif
187 	debuglen = sizeof(debug);
188 	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
189 		err(1, "sysctl-get(%s)", oid);
190 	ndebug = debug;
191 	for (; argc > 1; argc--, argv++) {
192 		cp = argv[1];
193 		do {
194 			u_int bit;
195 
196 			if (*cp == '-') {
197 				cp++;
198 				op = -1;
199 			} else if (*cp == '+') {
200 				cp++;
201 				op = 1;
202 			} else
203 				op = 0;
204 			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
205 				tp++;
206 			bit = getflag(cp, tp-cp);
207 			if (op < 0)
208 				ndebug &= ~bit;
209 			else if (op > 0)
210 				ndebug |= bit;
211 			else {
212 				if (bit == 0) {
213 					if (isdigit(*cp))
214 						bit = strtoul(cp, NULL, 0);
215 					else
216 						errx(1, "unknown flag %.*s",
217 							tp-cp, cp);
218 				}
219 				ndebug = bit;
220 			}
221 		} while (*(cp = tp) != '\0');
222 	}
223 	if (debug != ndebug) {
224 		printf("%s: 0x%x => ", oid, debug);
225 		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
226 			err(1, "sysctl-set(%s)", oid);
227 		printf("0x%x", ndebug);
228 		debug = ndebug;
229 	} else
230 		printf("%s: 0x%x", oid, debug);
231 	sep = "<";
232 	for (i = 0; i < N(flags); i++)
233 		if (debug & flags[i].bit) {
234 			printf("%s%s", sep, flags[i].name);
235 			sep = ",";
236 		}
237 	printf("%s\n", *sep != '<' ? ">" : "");
238 	return 0;
239 }
240