wlandebug.c revision 170534
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 170534 2007-06-11 04:05:15Z 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 */
83153317Ssam
84153317Ssamstatic struct {
85153317Ssam	const char	*name;
86153317Ssam	u_int		bit;
87153317Ssam} flags[] = {
88170534Ssam	{ "11n",	IEEE80211_MSG_11N },
89153317Ssam	{ "debug",	IEEE80211_MSG_DEBUG },
90153317Ssam	{ "dumppkts",	IEEE80211_MSG_DUMPPKTS },
91153317Ssam	{ "crypto",	IEEE80211_MSG_CRYPTO },
92153317Ssam	{ "input",	IEEE80211_MSG_INPUT },
93153317Ssam	{ "xrate",	IEEE80211_MSG_XRATE },
94153317Ssam	{ "elemid",	IEEE80211_MSG_ELEMID },
95153317Ssam	{ "node",	IEEE80211_MSG_NODE },
96153317Ssam	{ "assoc",	IEEE80211_MSG_ASSOC },
97153317Ssam	{ "auth",	IEEE80211_MSG_AUTH },
98153317Ssam	{ "scan",	IEEE80211_MSG_SCAN },
99153317Ssam	{ "output",	IEEE80211_MSG_OUTPUT },
100153317Ssam	{ "state",	IEEE80211_MSG_STATE },
101153317Ssam	{ "power",	IEEE80211_MSG_POWER },
102165963Ssam	{ "dot1x",	IEEE80211_MSG_DOT1X },
103153317Ssam	{ "dot1xsm",	IEEE80211_MSG_DOT1XSM },
104153317Ssam	{ "radius",	IEEE80211_MSG_RADIUS },
105153317Ssam	{ "raddump",	IEEE80211_MSG_RADDUMP },
106153317Ssam	{ "radkeys",	IEEE80211_MSG_RADKEYS },
107153317Ssam	{ "wpa",	IEEE80211_MSG_WPA },
108153317Ssam	{ "acl",	IEEE80211_MSG_ACL },
109153317Ssam	{ "wme",	IEEE80211_MSG_WME },
110153317Ssam	{ "superg",	IEEE80211_MSG_SUPERG },
111153317Ssam	{ "doth",	IEEE80211_MSG_DOTH },
112153317Ssam	{ "inact",	IEEE80211_MSG_INACT },
113153317Ssam	{ "roam",	IEEE80211_MSG_ROAM },
114164635Ssam	{ "rate",	IEEE80211_MSG_RATECTL },
115153317Ssam};
116153317Ssam
117153317Ssamstatic u_int
118153317Ssamgetflag(const char *name, int len)
119153317Ssam{
120153317Ssam	int i;
121153317Ssam
122153317Ssam	for (i = 0; i < N(flags); i++)
123153317Ssam		if (strncasecmp(flags[i].name, name, len) == 0)
124153317Ssam			return flags[i].bit;
125153317Ssam	return 0;
126153317Ssam}
127153317Ssam
128153317Ssamstatic const char *
129153317Ssamgetflagname(u_int flag)
130153317Ssam{
131153317Ssam	int i;
132153317Ssam
133153317Ssam	for (i = 0; i < N(flags); i++)
134153317Ssam		if (flags[i].bit == flag)
135153317Ssam			return flags[i].name;
136153317Ssam	return "???";
137153317Ssam}
138153317Ssam
139153317Ssamstatic void
140153317Ssamusage(void)
141153317Ssam{
142153317Ssam	int i;
143153317Ssam
144153317Ssam	fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
145153317Ssam	fprintf(stderr, "where flags are:\n");
146153317Ssam	for (i = 0; i < N(flags); i++)
147153317Ssam		printf("%s\n", flags[i].name);
148153317Ssam	exit(-1);
149153317Ssam}
150153317Ssam
151153317Ssamint
152153317Ssammain(int argc, char *argv[])
153153317Ssam{
154153317Ssam	const char *ifname = "ath0";
155153317Ssam	const char *cp, *tp;
156153317Ssam	const char *sep;
157170534Ssam	int op, i, unit;
158153317Ssam	u_int32_t debug, ndebug;
159153317Ssam	size_t debuglen, parentlen;
160153317Ssam	char oid[256], parent[256];
161153317Ssam
162153317Ssam	progname = argv[0];
163153317Ssam	if (argc > 1) {
164153317Ssam		if (strcmp(argv[1], "-i") == 0) {
165153317Ssam			if (argc < 2)
166153317Ssam				errx(1, "missing interface name for -i option");
167153317Ssam			ifname = argv[2];
168153317Ssam			argc -= 2, argv += 2;
169153317Ssam		} else if (strcmp(argv[1], "-?") == 0)
170153317Ssam			usage();
171153317Ssam	}
172153317Ssam
173153317Ssam	for (unit = 0; unit < 10; unit++) {
174153317Ssam#ifdef __linux__
175153317Ssam		snprintf(oid, sizeof(oid), "net.wlan%d.%%parent", unit);
176153317Ssam#else
177153317Ssam		snprintf(oid, sizeof(oid), "net.wlan.%d.%%parent", unit);
178153317Ssam#endif
179153317Ssam		parentlen = sizeof(parent);
180170534Ssam		if (sysctlbyname(oid, parent, &parentlen, NULL, 0) < 0)
181170534Ssam			continue;
182170534Ssam		if (strncmp(parent, ifname, parentlen) == 0)
183153317Ssam			break;
184153317Ssam	}
185153317Ssam	if (unit == 10)
186153317Ssam		errx(1, "%s: cannot locate wlan sysctl node.", ifname);
187153317Ssam#ifdef __linux__
188153317Ssam	snprintf(oid, sizeof(oid), "net.wlan%d.debug", unit);
189153317Ssam#else
190153317Ssam	snprintf(oid, sizeof(oid), "net.wlan.%d.debug", unit);
191153317Ssam#endif
192153317Ssam	debuglen = sizeof(debug);
193153317Ssam	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
194153317Ssam		err(1, "sysctl-get(%s)", oid);
195153317Ssam	ndebug = debug;
196153317Ssam	for (; argc > 1; argc--, argv++) {
197153317Ssam		cp = argv[1];
198153317Ssam		do {
199153317Ssam			u_int bit;
200153317Ssam
201153317Ssam			if (*cp == '-') {
202153317Ssam				cp++;
203153317Ssam				op = -1;
204153317Ssam			} else if (*cp == '+') {
205153317Ssam				cp++;
206153317Ssam				op = 1;
207153317Ssam			} else
208153317Ssam				op = 0;
209153317Ssam			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
210153317Ssam				tp++;
211153317Ssam			bit = getflag(cp, tp-cp);
212153317Ssam			if (op < 0)
213153317Ssam				ndebug &= ~bit;
214153317Ssam			else if (op > 0)
215153317Ssam				ndebug |= bit;
216153317Ssam			else {
217153317Ssam				if (bit == 0) {
218170534Ssam					int c = *cp;
219170534Ssam					if (isdigit(c))
220153317Ssam						bit = strtoul(cp, NULL, 0);
221153317Ssam					else
222153317Ssam						errx(1, "unknown flag %.*s",
223170534Ssam							(int)(tp-cp), cp);
224153317Ssam				}
225153317Ssam				ndebug = bit;
226153317Ssam			}
227153317Ssam		} while (*(cp = tp) != '\0');
228153317Ssam	}
229153317Ssam	if (debug != ndebug) {
230153317Ssam		printf("%s: 0x%x => ", oid, debug);
231153317Ssam		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
232153317Ssam			err(1, "sysctl-set(%s)", oid);
233153317Ssam		printf("0x%x", ndebug);
234153317Ssam		debug = ndebug;
235153317Ssam	} else
236153317Ssam		printf("%s: 0x%x", oid, debug);
237153317Ssam	sep = "<";
238153317Ssam	for (i = 0; i < N(flags); i++)
239153317Ssam		if (debug & flags[i].bit) {
240153317Ssam			printf("%s%s", sep, flags[i].name);
241153317Ssam			sep = ",";
242153317Ssam		}
243153317Ssam	printf("%s\n", *sep != '<' ? ">" : "");
244153317Ssam	return 0;
245153317Ssam}
246