xref: /freebsd/usr.sbin/wlandebug/wlandebug.c (revision 780fb4a2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * $FreeBSD$
32  */
33 
34 /*
35  * wlandebug [-i interface] flags
36  * (default interface is wlan.0).
37  */
38 #include <sys/types.h>
39 #include <sys/sysctl.h>
40 
41 #include <net/if.h>
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <ctype.h>
46 #include <getopt.h>
47 #include <string.h>
48 #include <err.h>
49 
50 #include <libifconfig.h>
51 
52 #define	N(a)	(sizeof(a)/sizeof(a[0]))
53 
54 const char *progname;
55 
56 #define	IEEE80211_MSG_11N	0x80000000	/* 11n mode debug */
57 #define	IEEE80211_MSG_DEBUG	0x40000000	/* IFF_DEBUG equivalent */
58 #define	IEEE80211_MSG_DUMPPKTS	0x20000000	/* IFF_LINK2 equivalant */
59 #define	IEEE80211_MSG_CRYPTO	0x10000000	/* crypto work */
60 #define	IEEE80211_MSG_INPUT	0x08000000	/* input handling */
61 #define	IEEE80211_MSG_XRATE	0x04000000	/* rate set handling */
62 #define	IEEE80211_MSG_ELEMID	0x02000000	/* element id parsing */
63 #define	IEEE80211_MSG_NODE	0x01000000	/* node handling */
64 #define	IEEE80211_MSG_ASSOC	0x00800000	/* association handling */
65 #define	IEEE80211_MSG_AUTH	0x00400000	/* authentication handling */
66 #define	IEEE80211_MSG_SCAN	0x00200000	/* scanning */
67 #define	IEEE80211_MSG_OUTPUT	0x00100000	/* output handling */
68 #define	IEEE80211_MSG_STATE	0x00080000	/* state machine */
69 #define	IEEE80211_MSG_POWER	0x00040000	/* power save handling */
70 #define	IEEE80211_MSG_HWMP	0x00020000	/* hybrid mesh protocol */
71 #define	IEEE80211_MSG_DOT1XSM	0x00010000	/* 802.1x state machine */
72 #define	IEEE80211_MSG_RADIUS	0x00008000	/* 802.1x radius client */
73 #define	IEEE80211_MSG_RADDUMP	0x00004000	/* dump 802.1x radius packets */
74 #define	IEEE80211_MSG_MESH	0x00002000	/* mesh networking */
75 #define	IEEE80211_MSG_WPA	0x00001000	/* WPA/RSN protocol */
76 #define	IEEE80211_MSG_ACL	0x00000800	/* ACL handling */
77 #define	IEEE80211_MSG_WME	0x00000400	/* WME protocol */
78 #define	IEEE80211_MSG_SUPERG	0x00000200	/* Atheros SuperG protocol */
79 #define	IEEE80211_MSG_DOTH	0x00000100	/* 802.11h support */
80 #define	IEEE80211_MSG_INACT	0x00000080	/* inactivity handling */
81 #define	IEEE80211_MSG_ROAM	0x00000040	/* sta-mode roaming */
82 #define	IEEE80211_MSG_RATECTL	0x00000020	/* tx rate control */
83 #define	IEEE80211_MSG_ACTION	0x00000010	/* action frame handling */
84 #define	IEEE80211_MSG_WDS	0x00000008	/* WDS handling */
85 #define	IEEE80211_MSG_IOCTL	0x00000004	/* ioctl handling */
86 #define	IEEE80211_MSG_TDMA	0x00000002	/* TDMA handling */
87 
88 static struct {
89 	const char	*name;
90 	u_int		bit;
91 } flags[] = {
92 	{ "11n",	IEEE80211_MSG_11N },
93 	{ "debug",	IEEE80211_MSG_DEBUG },
94 	{ "dumppkts",	IEEE80211_MSG_DUMPPKTS },
95 	{ "crypto",	IEEE80211_MSG_CRYPTO },
96 	{ "input",	IEEE80211_MSG_INPUT },
97 	{ "xrate",	IEEE80211_MSG_XRATE },
98 	{ "elemid",	IEEE80211_MSG_ELEMID },
99 	{ "node",	IEEE80211_MSG_NODE },
100 	{ "assoc",	IEEE80211_MSG_ASSOC },
101 	{ "auth",	IEEE80211_MSG_AUTH },
102 	{ "scan",	IEEE80211_MSG_SCAN },
103 	{ "output",	IEEE80211_MSG_OUTPUT },
104 	{ "state",	IEEE80211_MSG_STATE },
105 	{ "power",	IEEE80211_MSG_POWER },
106 	{ "hwmp",	IEEE80211_MSG_HWMP },
107 	{ "dot1xsm",	IEEE80211_MSG_DOT1XSM },
108 	{ "radius",	IEEE80211_MSG_RADIUS },
109 	{ "raddump",	IEEE80211_MSG_RADDUMP },
110 	{ "mesh",	IEEE80211_MSG_MESH },
111 	{ "wpa",	IEEE80211_MSG_WPA },
112 	{ "acl",	IEEE80211_MSG_ACL },
113 	{ "wme",	IEEE80211_MSG_WME },
114 	{ "superg",	IEEE80211_MSG_SUPERG },
115 	{ "doth",	IEEE80211_MSG_DOTH },
116 	{ "inact",	IEEE80211_MSG_INACT },
117 	{ "roam",	IEEE80211_MSG_ROAM },
118 	{ "rate",	IEEE80211_MSG_RATECTL },
119 	{ "action",	IEEE80211_MSG_ACTION },
120 	{ "wds",	IEEE80211_MSG_WDS },
121 	{ "ioctl",	IEEE80211_MSG_IOCTL },
122 	{ "tdma",	IEEE80211_MSG_TDMA },
123 };
124 
125 static u_int
126 getflag(const char *name, int len)
127 {
128 	int i;
129 
130 	for (i = 0; i < N(flags); i++)
131 		if (strncasecmp(flags[i].name, name, len) == 0)
132 			return flags[i].bit;
133 	return 0;
134 }
135 
136 static void
137 usage(void)
138 {
139 	int i;
140 
141 	fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname);
142 	fprintf(stderr, "where flags are:\n");
143 	for (i = 0; i < N(flags); i++)
144 		printf("%s\n", flags[i].name);
145 	exit(-1);
146 }
147 
148 static void
149 setoid(char oid[], size_t oidlen, const char *wlan)
150 {
151 #ifdef __linux__
152 	if (wlan)
153 		snprintf(oid, oidlen, "net.%s.debug", wlan);
154 #elif __FreeBSD__
155 	if (wlan)
156 		snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4);
157 	else
158 		snprintf(oid, oidlen, "net.wlan.debug");
159 #elif __NetBSD__
160 	if (wlan)
161 		snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan);
162 	else
163 		snprintf(oid, oidlen, "net.link.ieee80211.debug");
164 #else
165 #error "No support for this system"
166 #endif
167 }
168 
169 static void
170 get_orig_iface_name(char *oid, size_t oid_size, char *name)
171 {
172 	struct ifconfig_handle *h;
173 	char *orig_name;
174 
175 	h = ifconfig_open();
176 	if (ifconfig_get_orig_name(h, name, &orig_name) < 0) {
177 		/* check for original interface name. */
178 		orig_name = name;
179 	}
180 
181 	if (strlen(orig_name) < strlen("wlan") + 1 ||
182 	    strncmp(orig_name, "wlan", 4) != 0)
183 		errx(1, "expecting a wlan interface name");
184 
185 	ifconfig_close(h);
186 	setoid(oid, oid_size, orig_name);
187 	if (orig_name != name)
188 		free(orig_name);
189 }
190 
191 int
192 main(int argc, char *argv[])
193 {
194 	const char *cp, *tp;
195 	const char *sep;
196 	int op, i;
197 	u_int32_t debug, ndebug;
198 	size_t debuglen;
199 	char oid[256];
200 
201 	progname = argv[0];
202 	setoid(oid, sizeof(oid), "wlan0");
203 	if (argc > 1) {
204 		if (strcmp(argv[1], "-d") == 0) {
205 			setoid(oid, sizeof(oid), NULL);
206 			argc -= 1, argv += 1;
207 		} else if (strcmp(argv[1], "-i") == 0) {
208 			if (argc <= 2)
209 				errx(1, "missing interface name for -i option");
210 			get_orig_iface_name(oid, sizeof(oid), argv[2]);
211 			argc -= 2, argv += 2;
212 		} else if (strcmp(argv[1], "-?") == 0)
213 			usage();
214 	}
215 
216 	debuglen = sizeof(debug);
217 	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
218 		err(1, "sysctl-get(%s)", oid);
219 	ndebug = debug;
220 	for (; argc > 1; argc--, argv++) {
221 		cp = argv[1];
222 		do {
223 			u_int bit;
224 
225 			if (*cp == '-') {
226 				cp++;
227 				op = -1;
228 			} else if (*cp == '+') {
229 				cp++;
230 				op = 1;
231 			} else
232 				op = 0;
233 			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
234 				tp++;
235 			bit = getflag(cp, tp-cp);
236 			if (op < 0)
237 				ndebug &= ~bit;
238 			else if (op > 0)
239 				ndebug |= bit;
240 			else {
241 				if (bit == 0) {
242 					int c = *cp;
243 					if (isdigit(c))
244 						bit = strtoul(cp, NULL, 0);
245 					else
246 						errx(1, "unknown flag %.*s",
247 							(int)(tp-cp), cp);
248 				}
249 				ndebug = bit;
250 			}
251 		} while (*(cp = tp) != '\0');
252 	}
253 	if (debug != ndebug) {
254 		printf("%s: 0x%x => ", oid, debug);
255 		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
256 			err(1, "sysctl-set(%s)", oid);
257 		printf("0x%x", ndebug);
258 		debug = ndebug;
259 	} else
260 		printf("%s: 0x%x", oid, debug);
261 	sep = "<";
262 	for (i = 0; i < N(flags); i++)
263 		if (debug & flags[i].bit) {
264 			printf("%s%s", sep, flags[i].name);
265 			sep = ",";
266 		}
267 	printf("%s\n", *sep != '<' ? ">" : "");
268 	return 0;
269 }
270