1153317Ssam/*-
2186904Ssam * Copyright (c) 2002-2009 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 *
16153317Ssam * NO WARRANTY
17153317Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18153317Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19153317Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20153317Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21153317Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22153317Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23153317Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24153317Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25153317Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26153317Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27153317Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28153317Ssam *
29153317Ssam * $FreeBSD$
30153317Ssam */
31153317Ssam
32153317Ssam/*
33153317Ssam * wlandebug [-i interface] flags
34153317Ssam * (default interface is wlan.0).
35153317Ssam */
36153317Ssam#include <sys/types.h>
37179398Ssam#include <sys/sysctl.h>
38153317Ssam
39153317Ssam#include <stdio.h>
40170534Ssam#include <stdlib.h>
41153317Ssam#include <ctype.h>
42153317Ssam#include <getopt.h>
43153317Ssam#include <string.h>
44170534Ssam#include <err.h>
45153317Ssam
46153317Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
47153317Ssam
48153317Ssamconst char *progname;
49153317Ssam
50170534Ssam#define	IEEE80211_MSG_11N	0x80000000	/* 11n mode debug */
51153317Ssam#define	IEEE80211_MSG_DEBUG	0x40000000	/* IFF_DEBUG equivalent */
52153317Ssam#define	IEEE80211_MSG_DUMPPKTS	0x20000000	/* IFF_LINK2 equivalant */
53153317Ssam#define	IEEE80211_MSG_CRYPTO	0x10000000	/* crypto work */
54153317Ssam#define	IEEE80211_MSG_INPUT	0x08000000	/* input handling */
55153317Ssam#define	IEEE80211_MSG_XRATE	0x04000000	/* rate set handling */
56153317Ssam#define	IEEE80211_MSG_ELEMID	0x02000000	/* element id parsing */
57153317Ssam#define	IEEE80211_MSG_NODE	0x01000000	/* node handling */
58153317Ssam#define	IEEE80211_MSG_ASSOC	0x00800000	/* association handling */
59153317Ssam#define	IEEE80211_MSG_AUTH	0x00400000	/* authentication handling */
60153317Ssam#define	IEEE80211_MSG_SCAN	0x00200000	/* scanning */
61153317Ssam#define	IEEE80211_MSG_OUTPUT	0x00100000	/* output handling */
62153317Ssam#define	IEEE80211_MSG_STATE	0x00080000	/* state machine */
63153317Ssam#define	IEEE80211_MSG_POWER	0x00040000	/* power save handling */
64195746Ssam#define	IEEE80211_MSG_HWMP	0x00020000	/* hybrid mesh protocol */
65153317Ssam#define	IEEE80211_MSG_DOT1XSM	0x00010000	/* 802.1x state machine */
66153317Ssam#define	IEEE80211_MSG_RADIUS	0x00008000	/* 802.1x radius client */
67153317Ssam#define	IEEE80211_MSG_RADDUMP	0x00004000	/* dump 802.1x radius packets */
68195746Ssam#define	IEEE80211_MSG_MESH	0x00002000	/* mesh networking */
69153317Ssam#define	IEEE80211_MSG_WPA	0x00001000	/* WPA/RSN protocol */
70153317Ssam#define	IEEE80211_MSG_ACL	0x00000800	/* ACL handling */
71153317Ssam#define	IEEE80211_MSG_WME	0x00000400	/* WME protocol */
72153317Ssam#define	IEEE80211_MSG_SUPERG	0x00000200	/* Atheros SuperG protocol */
73153317Ssam#define	IEEE80211_MSG_DOTH	0x00000100	/* 802.11h support */
74153317Ssam#define	IEEE80211_MSG_INACT	0x00000080	/* inactivity handling */
75153317Ssam#define	IEEE80211_MSG_ROAM	0x00000040	/* sta-mode roaming */
76164635Ssam#define	IEEE80211_MSG_RATECTL	0x00000020	/* tx rate control */
77178359Ssam#define	IEEE80211_MSG_ACTION	0x00000010	/* action frame handling */
78178359Ssam#define	IEEE80211_MSG_WDS	0x00000008	/* WDS handling */
79178359Ssam#define	IEEE80211_MSG_IOCTL	0x00000004	/* ioctl handling */
80186904Ssam#define	IEEE80211_MSG_TDMA	0x00000002	/* TDMA handling */
81153317Ssam
82153317Ssamstatic struct {
83153317Ssam	const char	*name;
84153317Ssam	u_int		bit;
85153317Ssam} flags[] = {
86170534Ssam	{ "11n",	IEEE80211_MSG_11N },
87153317Ssam	{ "debug",	IEEE80211_MSG_DEBUG },
88153317Ssam	{ "dumppkts",	IEEE80211_MSG_DUMPPKTS },
89153317Ssam	{ "crypto",	IEEE80211_MSG_CRYPTO },
90153317Ssam	{ "input",	IEEE80211_MSG_INPUT },
91153317Ssam	{ "xrate",	IEEE80211_MSG_XRATE },
92153317Ssam	{ "elemid",	IEEE80211_MSG_ELEMID },
93153317Ssam	{ "node",	IEEE80211_MSG_NODE },
94153317Ssam	{ "assoc",	IEEE80211_MSG_ASSOC },
95153317Ssam	{ "auth",	IEEE80211_MSG_AUTH },
96153317Ssam	{ "scan",	IEEE80211_MSG_SCAN },
97153317Ssam	{ "output",	IEEE80211_MSG_OUTPUT },
98153317Ssam	{ "state",	IEEE80211_MSG_STATE },
99153317Ssam	{ "power",	IEEE80211_MSG_POWER },
100195746Ssam	{ "hwmp",	IEEE80211_MSG_HWMP },
101153317Ssam	{ "dot1xsm",	IEEE80211_MSG_DOT1XSM },
102153317Ssam	{ "radius",	IEEE80211_MSG_RADIUS },
103153317Ssam	{ "raddump",	IEEE80211_MSG_RADDUMP },
104195746Ssam	{ "mesh",	IEEE80211_MSG_MESH },
105153317Ssam	{ "wpa",	IEEE80211_MSG_WPA },
106153317Ssam	{ "acl",	IEEE80211_MSG_ACL },
107153317Ssam	{ "wme",	IEEE80211_MSG_WME },
108153317Ssam	{ "superg",	IEEE80211_MSG_SUPERG },
109153317Ssam	{ "doth",	IEEE80211_MSG_DOTH },
110153317Ssam	{ "inact",	IEEE80211_MSG_INACT },
111153317Ssam	{ "roam",	IEEE80211_MSG_ROAM },
112164635Ssam	{ "rate",	IEEE80211_MSG_RATECTL },
113178359Ssam	{ "action",	IEEE80211_MSG_ACTION },
114178359Ssam	{ "wds",	IEEE80211_MSG_WDS },
115178359Ssam	{ "ioctl",	IEEE80211_MSG_IOCTL },
116186904Ssam	{ "tdma",	IEEE80211_MSG_TDMA },
117153317Ssam};
118153317Ssam
119153317Ssamstatic u_int
120153317Ssamgetflag(const char *name, int len)
121153317Ssam{
122153317Ssam	int i;
123153317Ssam
124153317Ssam	for (i = 0; i < N(flags); i++)
125153317Ssam		if (strncasecmp(flags[i].name, name, len) == 0)
126153317Ssam			return flags[i].bit;
127153317Ssam	return 0;
128153317Ssam}
129153317Ssam
130153317Ssamstatic void
131153317Ssamusage(void)
132153317Ssam{
133153317Ssam	int i;
134153317Ssam
135179121Sthompsa	fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname);
136153317Ssam	fprintf(stderr, "where flags are:\n");
137153317Ssam	for (i = 0; i < N(flags); i++)
138153317Ssam		printf("%s\n", flags[i].name);
139153317Ssam	exit(-1);
140153317Ssam}
141153317Ssam
142178359Ssamstatic void
143178359Ssamsetoid(char oid[], size_t oidlen, const char *wlan)
144178359Ssam{
145178359Ssam#ifdef __linux__
146179121Sthompsa	if (wlan)
147179121Sthompsa		snprintf(oid, oidlen, "net.%s.debug", wlan);
148178359Ssam#elif __FreeBSD__
149179121Sthompsa	if (wlan)
150179121Sthompsa		snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4);
151179121Sthompsa	else
152179121Sthompsa		snprintf(oid, oidlen, "net.wlan.debug");
153178359Ssam#elif __NetBSD__
154179121Sthompsa	if (wlan)
155179398Ssam		snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan);
156179121Sthompsa	else
157179121Sthompsa		snprintf(oid, oidlen, "net.link.ieee80211.debug");
158178359Ssam#else
159178359Ssam#error "No support for this system"
160178359Ssam#endif
161178359Ssam}
162178359Ssam
163153317Ssamint
164153317Ssammain(int argc, char *argv[])
165153317Ssam{
166153317Ssam	const char *cp, *tp;
167153317Ssam	const char *sep;
168179398Ssam	int op, i;
169153317Ssam	u_int32_t debug, ndebug;
170179398Ssam	size_t debuglen;
171178359Ssam	char oid[256];
172153317Ssam
173153317Ssam	progname = argv[0];
174178359Ssam	setoid(oid, sizeof(oid), "wlan0");
175153317Ssam	if (argc > 1) {
176179121Sthompsa		if (strcmp(argv[1], "-d") == 0) {
177179121Sthompsa			setoid(oid, sizeof(oid), NULL);
178179121Sthompsa			argc -= 1, argv += 1;
179179121Sthompsa		} else if (strcmp(argv[1], "-i") == 0) {
180153317Ssam			if (argc < 2)
181153317Ssam				errx(1, "missing interface name for -i option");
182178359Ssam			if (strncmp(argv[2], "wlan", 4) != 0)
183178359Ssam				errx(1, "expecting a wlan interface name");
184178359Ssam			setoid(oid, sizeof(oid), argv[2]);
185153317Ssam			argc -= 2, argv += 2;
186153317Ssam		} else if (strcmp(argv[1], "-?") == 0)
187153317Ssam			usage();
188153317Ssam	}
189153317Ssam
190153317Ssam	debuglen = sizeof(debug);
191153317Ssam	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
192153317Ssam		err(1, "sysctl-get(%s)", oid);
193153317Ssam	ndebug = debug;
194153317Ssam	for (; argc > 1; argc--, argv++) {
195153317Ssam		cp = argv[1];
196153317Ssam		do {
197153317Ssam			u_int bit;
198153317Ssam
199153317Ssam			if (*cp == '-') {
200153317Ssam				cp++;
201153317Ssam				op = -1;
202153317Ssam			} else if (*cp == '+') {
203153317Ssam				cp++;
204153317Ssam				op = 1;
205153317Ssam			} else
206153317Ssam				op = 0;
207153317Ssam			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
208153317Ssam				tp++;
209153317Ssam			bit = getflag(cp, tp-cp);
210153317Ssam			if (op < 0)
211153317Ssam				ndebug &= ~bit;
212153317Ssam			else if (op > 0)
213153317Ssam				ndebug |= bit;
214153317Ssam			else {
215153317Ssam				if (bit == 0) {
216170534Ssam					int c = *cp;
217170534Ssam					if (isdigit(c))
218153317Ssam						bit = strtoul(cp, NULL, 0);
219153317Ssam					else
220153317Ssam						errx(1, "unknown flag %.*s",
221170534Ssam							(int)(tp-cp), cp);
222153317Ssam				}
223153317Ssam				ndebug = bit;
224153317Ssam			}
225153317Ssam		} while (*(cp = tp) != '\0');
226153317Ssam	}
227153317Ssam	if (debug != ndebug) {
228153317Ssam		printf("%s: 0x%x => ", oid, debug);
229153317Ssam		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
230153317Ssam			err(1, "sysctl-set(%s)", oid);
231153317Ssam		printf("0x%x", ndebug);
232153317Ssam		debug = ndebug;
233153317Ssam	} else
234153317Ssam		printf("%s: 0x%x", oid, debug);
235153317Ssam	sep = "<";
236153317Ssam	for (i = 0; i < N(flags); i++)
237153317Ssam		if (debug & flags[i].bit) {
238153317Ssam			printf("%s%s", sep, flags[i].name);
239153317Ssam			sep = ",";
240153317Ssam		}
241153317Ssam	printf("%s\n", *sep != '<' ? ">" : "");
242153317Ssam	return 0;
243153317Ssam}
244