1153317Ssam/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5153317Ssam * All rights reserved.
6153317Ssam *
7153317Ssam * Redistribution and use in source and binary forms, with or without
8153317Ssam * modification, are permitted provided that the following conditions
9153317Ssam * are met:
10153317Ssam * 1. Redistributions of source code must retain the above copyright
11153317Ssam *    notice, this list of conditions and the following disclaimer,
12153317Ssam *    without modification.
13153317Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14153317Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15153317Ssam *    redistribution must be conditioned upon including a substantially
16153317Ssam *    similar Disclaimer requirement for further binary redistribution.
17153317Ssam *
18153317Ssam * NO WARRANTY
19153317Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20153317Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21153317Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22153317Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23153317Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24153317Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25153317Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26153317Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27153317Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28153317Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29153317Ssam * THE POSSIBILITY OF SUCH DAMAGES.
30153317Ssam *
31153317Ssam * $FreeBSD: stable/11/usr.sbin/wlandebug/wlandebug.c 330449 2018-03-05 07:26:05Z eadler $
32153317Ssam */
33153317Ssam
34153317Ssam/*
35153317Ssam * wlandebug [-i interface] flags
36153317Ssam * (default interface is wlan.0).
37153317Ssam */
38153317Ssam#include <sys/types.h>
39179398Ssam#include <sys/sysctl.h>
40153317Ssam
41153317Ssam#include <stdio.h>
42170534Ssam#include <stdlib.h>
43153317Ssam#include <ctype.h>
44153317Ssam#include <getopt.h>
45153317Ssam#include <string.h>
46170534Ssam#include <err.h>
47153317Ssam
48153317Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
49153317Ssam
50153317Ssamconst char *progname;
51153317Ssam
52170534Ssam#define	IEEE80211_MSG_11N	0x80000000	/* 11n mode debug */
53153317Ssam#define	IEEE80211_MSG_DEBUG	0x40000000	/* IFF_DEBUG equivalent */
54153317Ssam#define	IEEE80211_MSG_DUMPPKTS	0x20000000	/* IFF_LINK2 equivalant */
55153317Ssam#define	IEEE80211_MSG_CRYPTO	0x10000000	/* crypto work */
56153317Ssam#define	IEEE80211_MSG_INPUT	0x08000000	/* input handling */
57153317Ssam#define	IEEE80211_MSG_XRATE	0x04000000	/* rate set handling */
58153317Ssam#define	IEEE80211_MSG_ELEMID	0x02000000	/* element id parsing */
59153317Ssam#define	IEEE80211_MSG_NODE	0x01000000	/* node handling */
60153317Ssam#define	IEEE80211_MSG_ASSOC	0x00800000	/* association handling */
61153317Ssam#define	IEEE80211_MSG_AUTH	0x00400000	/* authentication handling */
62153317Ssam#define	IEEE80211_MSG_SCAN	0x00200000	/* scanning */
63153317Ssam#define	IEEE80211_MSG_OUTPUT	0x00100000	/* output handling */
64153317Ssam#define	IEEE80211_MSG_STATE	0x00080000	/* state machine */
65153317Ssam#define	IEEE80211_MSG_POWER	0x00040000	/* power save handling */
66195746Ssam#define	IEEE80211_MSG_HWMP	0x00020000	/* hybrid mesh protocol */
67153317Ssam#define	IEEE80211_MSG_DOT1XSM	0x00010000	/* 802.1x state machine */
68153317Ssam#define	IEEE80211_MSG_RADIUS	0x00008000	/* 802.1x radius client */
69153317Ssam#define	IEEE80211_MSG_RADDUMP	0x00004000	/* dump 802.1x radius packets */
70195746Ssam#define	IEEE80211_MSG_MESH	0x00002000	/* mesh networking */
71153317Ssam#define	IEEE80211_MSG_WPA	0x00001000	/* WPA/RSN protocol */
72153317Ssam#define	IEEE80211_MSG_ACL	0x00000800	/* ACL handling */
73153317Ssam#define	IEEE80211_MSG_WME	0x00000400	/* WME protocol */
74153317Ssam#define	IEEE80211_MSG_SUPERG	0x00000200	/* Atheros SuperG protocol */
75153317Ssam#define	IEEE80211_MSG_DOTH	0x00000100	/* 802.11h support */
76153317Ssam#define	IEEE80211_MSG_INACT	0x00000080	/* inactivity handling */
77153317Ssam#define	IEEE80211_MSG_ROAM	0x00000040	/* sta-mode roaming */
78164635Ssam#define	IEEE80211_MSG_RATECTL	0x00000020	/* tx rate control */
79178359Ssam#define	IEEE80211_MSG_ACTION	0x00000010	/* action frame handling */
80178359Ssam#define	IEEE80211_MSG_WDS	0x00000008	/* WDS handling */
81178359Ssam#define	IEEE80211_MSG_IOCTL	0x00000004	/* ioctl handling */
82186904Ssam#define	IEEE80211_MSG_TDMA	0x00000002	/* TDMA handling */
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 },
102195746Ssam	{ "hwmp",	IEEE80211_MSG_HWMP },
103153317Ssam	{ "dot1xsm",	IEEE80211_MSG_DOT1XSM },
104153317Ssam	{ "radius",	IEEE80211_MSG_RADIUS },
105153317Ssam	{ "raddump",	IEEE80211_MSG_RADDUMP },
106195746Ssam	{ "mesh",	IEEE80211_MSG_MESH },
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 },
115178359Ssam	{ "action",	IEEE80211_MSG_ACTION },
116178359Ssam	{ "wds",	IEEE80211_MSG_WDS },
117178359Ssam	{ "ioctl",	IEEE80211_MSG_IOCTL },
118186904Ssam	{ "tdma",	IEEE80211_MSG_TDMA },
119153317Ssam};
120153317Ssam
121153317Ssamstatic u_int
122153317Ssamgetflag(const char *name, int len)
123153317Ssam{
124153317Ssam	int i;
125153317Ssam
126153317Ssam	for (i = 0; i < N(flags); i++)
127153317Ssam		if (strncasecmp(flags[i].name, name, len) == 0)
128153317Ssam			return flags[i].bit;
129153317Ssam	return 0;
130153317Ssam}
131153317Ssam
132153317Ssamstatic void
133153317Ssamusage(void)
134153317Ssam{
135153317Ssam	int i;
136153317Ssam
137179121Sthompsa	fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname);
138153317Ssam	fprintf(stderr, "where flags are:\n");
139153317Ssam	for (i = 0; i < N(flags); i++)
140153317Ssam		printf("%s\n", flags[i].name);
141153317Ssam	exit(-1);
142153317Ssam}
143153317Ssam
144178359Ssamstatic void
145178359Ssamsetoid(char oid[], size_t oidlen, const char *wlan)
146178359Ssam{
147178359Ssam#ifdef __linux__
148179121Sthompsa	if (wlan)
149179121Sthompsa		snprintf(oid, oidlen, "net.%s.debug", wlan);
150178359Ssam#elif __FreeBSD__
151179121Sthompsa	if (wlan)
152179121Sthompsa		snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4);
153179121Sthompsa	else
154179121Sthompsa		snprintf(oid, oidlen, "net.wlan.debug");
155178359Ssam#elif __NetBSD__
156179121Sthompsa	if (wlan)
157179398Ssam		snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan);
158179121Sthompsa	else
159179121Sthompsa		snprintf(oid, oidlen, "net.link.ieee80211.debug");
160178359Ssam#else
161178359Ssam#error "No support for this system"
162178359Ssam#endif
163178359Ssam}
164178359Ssam
165153317Ssamint
166153317Ssammain(int argc, char *argv[])
167153317Ssam{
168153317Ssam	const char *cp, *tp;
169153317Ssam	const char *sep;
170179398Ssam	int op, i;
171153317Ssam	u_int32_t debug, ndebug;
172179398Ssam	size_t debuglen;
173178359Ssam	char oid[256];
174153317Ssam
175153317Ssam	progname = argv[0];
176178359Ssam	setoid(oid, sizeof(oid), "wlan0");
177153317Ssam	if (argc > 1) {
178179121Sthompsa		if (strcmp(argv[1], "-d") == 0) {
179179121Sthompsa			setoid(oid, sizeof(oid), NULL);
180179121Sthompsa			argc -= 1, argv += 1;
181179121Sthompsa		} else if (strcmp(argv[1], "-i") == 0) {
182270518Shiren			if (argc <= 2)
183153317Ssam				errx(1, "missing interface name for -i option");
184178359Ssam			if (strncmp(argv[2], "wlan", 4) != 0)
185178359Ssam				errx(1, "expecting a wlan interface name");
186178359Ssam			setoid(oid, sizeof(oid), argv[2]);
187153317Ssam			argc -= 2, argv += 2;
188153317Ssam		} else if (strcmp(argv[1], "-?") == 0)
189153317Ssam			usage();
190153317Ssam	}
191153317Ssam
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