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