121e06e4sam/*-
27551d83pfg * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
37551d83pfg *
498ad45csam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
521e06e4sam * All rights reserved.
621e06e4sam *
721e06e4sam * Redistribution and use in source and binary forms, with or without
821e06e4sam * modification, are permitted provided that the following conditions
921e06e4sam * are met:
1021e06e4sam * 1. Redistributions of source code must retain the above copyright
1121e06e4sam *    notice, this list of conditions and the following disclaimer,
1221e06e4sam *    without modification.
1321e06e4sam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1421e06e4sam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
1521e06e4sam *    redistribution must be conditioned upon including a substantially
1621e06e4sam *    similar Disclaimer requirement for further binary redistribution.
1721e06e4sam *
1821e06e4sam * NO WARRANTY
1921e06e4sam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2021e06e4sam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2121e06e4sam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
2221e06e4sam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
2321e06e4sam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
2421e06e4sam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2521e06e4sam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2621e06e4sam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2721e06e4sam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2821e06e4sam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2921e06e4sam * THE POSSIBILITY OF SUCH DAMAGES.
3021e06e4sam *
3121e06e4sam * $FreeBSD$
3221e06e4sam */
3321e06e4sam
3421e06e4sam/*
35310b3b7sam * wlandebug [-i interface] flags
3621e06e4sam * (default interface is wlan.0).
3721e06e4sam */
3821e06e4sam#include <sys/types.h>
391322406sam#include <sys/sysctl.h>
4021e06e4sam
410d688beasomers#include <net/if.h>
420d688beasomers
4321e06e4sam#include <stdio.h>
44ffbf3d6sam#include <stdlib.h>
4521e06e4sam#include <ctype.h>
4621e06e4sam#include <getopt.h>
4721e06e4sam#include <string.h>
48ffbf3d6sam#include <err.h>
4921e06e4sam
50b4413f0avos#include <libifconfig.h>
51b4413f0avos
5221e06e4sam#define	N(a)	(sizeof(a)/sizeof(a[0]))
5321e06e4sam
5421e06e4samconst char *progname;
5521e06e4sam
56ffbf3d6sam#define	IEEE80211_MSG_11N	0x80000000	/* 11n mode debug */
5721e06e4sam#define	IEEE80211_MSG_DEBUG	0x40000000	/* IFF_DEBUG equivalent */
5821e06e4sam#define	IEEE80211_MSG_DUMPPKTS	0x20000000	/* IFF_LINK2 equivalant */
5921e06e4sam#define	IEEE80211_MSG_CRYPTO	0x10000000	/* crypto work */
6021e06e4sam#define	IEEE80211_MSG_INPUT	0x08000000	/* input handling */
6121e06e4sam#define	IEEE80211_MSG_XRATE	0x04000000	/* rate set handling */
6221e06e4sam#define	IEEE80211_MSG_ELEMID	0x02000000	/* element id parsing */
6321e06e4sam#define	IEEE80211_MSG_NODE	0x01000000	/* node handling */
6421e06e4sam#define	IEEE80211_MSG_ASSOC	0x00800000	/* association handling */
6521e06e4sam#define	IEEE80211_MSG_AUTH	0x00400000	/* authentication handling */
6621e06e4sam#define	IEEE80211_MSG_SCAN	0x00200000	/* scanning */
6721e06e4sam#define	IEEE80211_MSG_OUTPUT	0x00100000	/* output handling */
6821e06e4sam#define	IEEE80211_MSG_STATE	0x00080000	/* state machine */
6921e06e4sam#define	IEEE80211_MSG_POWER	0x00040000	/* power save handling */
70f11433csam#define	IEEE80211_MSG_HWMP	0x00020000	/* hybrid mesh protocol */
7121e06e4sam#define	IEEE80211_MSG_DOT1XSM	0x00010000	/* 802.1x state machine */
7221e06e4sam#define	IEEE80211_MSG_RADIUS	0x00008000	/* 802.1x radius client */
7321e06e4sam#define	IEEE80211_MSG_RADDUMP	0x00004000	/* dump 802.1x radius packets */
74f11433csam#define	IEEE80211_MSG_MESH	0x00002000	/* mesh networking */
7521e06e4sam#define	IEEE80211_MSG_WPA	0x00001000	/* WPA/RSN protocol */
7621e06e4sam#define	IEEE80211_MSG_ACL	0x00000800	/* ACL handling */
7721e06e4sam#define	IEEE80211_MSG_WME	0x00000400	/* WME protocol */
78db1d886sam#define	IEEE80211_MSG_SUPERG	0x00000200	/* Atheros SuperG protocol */
79db1d886sam#define	IEEE80211_MSG_DOTH	0x00000100	/* 802.11h support */
80db1d886sam#define	IEEE80211_MSG_INACT	0x00000080	/* inactivity handling */
81db1d886sam#define	IEEE80211_MSG_ROAM	0x00000040	/* sta-mode roaming */
82e0945d6sam#define	IEEE80211_MSG_RATECTL	0x00000020	/* tx rate control */
83a56ef47sam#define	IEEE80211_MSG_ACTION	0x00000010	/* action frame handling */
84a56ef47sam#define	IEEE80211_MSG_WDS	0x00000008	/* WDS handling */
85a56ef47sam#define	IEEE80211_MSG_IOCTL	0x00000004	/* ioctl handling */
8698ad45csam#define	IEEE80211_MSG_TDMA	0x00000002	/* TDMA handling */
8721e06e4sam
8821e06e4samstatic struct {
8921e06e4sam	const char	*name;
9021e06e4sam	u_int		bit;
9121e06e4sam} flags[] = {
92ffbf3d6sam	{ "11n",	IEEE80211_MSG_11N },
9321e06e4sam	{ "debug",	IEEE80211_MSG_DEBUG },
9421e06e4sam	{ "dumppkts",	IEEE80211_MSG_DUMPPKTS },
9521e06e4sam	{ "crypto",	IEEE80211_MSG_CRYPTO },
9621e06e4sam	{ "input",	IEEE80211_MSG_INPUT },
9721e06e4sam	{ "xrate",	IEEE80211_MSG_XRATE },
9821e06e4sam	{ "elemid",	IEEE80211_MSG_ELEMID },
9921e06e4sam	{ "node",	IEEE80211_MSG_NODE },
10021e06e4sam	{ "assoc",	IEEE80211_MSG_ASSOC },
10121e06e4sam	{ "auth",	IEEE80211_MSG_AUTH },
10221e06e4sam	{ "scan",	IEEE80211_MSG_SCAN },
10321e06e4sam	{ "output",	IEEE80211_MSG_OUTPUT },
10421e06e4sam	{ "state",	IEEE80211_MSG_STATE },
10521e06e4sam	{ "power",	IEEE80211_MSG_POWER },
106f11433csam	{ "hwmp",	IEEE80211_MSG_HWMP },
10721e06e4sam	{ "dot1xsm",	IEEE80211_MSG_DOT1XSM },
10821e06e4sam	{ "radius",	IEEE80211_MSG_RADIUS },
10921e06e4sam	{ "raddump",	IEEE80211_MSG_RADDUMP },
110f11433csam	{ "mesh",	IEEE80211_MSG_MESH },
11121e06e4sam	{ "wpa",	IEEE80211_MSG_WPA },
11221e06e4sam	{ "acl",	IEEE80211_MSG_ACL },
11321e06e4sam	{ "wme",	IEEE80211_MSG_WME },
114db1d886sam	{ "superg",	IEEE80211_MSG_SUPERG },
115db1d886sam	{ "doth",	IEEE80211_MSG_DOTH },
116db1d886sam	{ "inact",	IEEE80211_MSG_INACT },
117db1d886sam	{ "roam",	IEEE80211_MSG_ROAM },
118e0945d6sam	{ "rate",	IEEE80211_MSG_RATECTL },
119a56ef47sam	{ "action",	IEEE80211_MSG_ACTION },
120a56ef47sam	{ "wds",	IEEE80211_MSG_WDS },
121a56ef47sam	{ "ioctl",	IEEE80211_MSG_IOCTL },
12298ad45csam	{ "tdma",	IEEE80211_MSG_TDMA },
12321e06e4sam};
12421e06e4sam
12521e06e4samstatic u_int
12621e06e4samgetflag(const char *name, int len)
12721e06e4sam{
12821e06e4sam	int i;
12921e06e4sam
13021e06e4sam	for (i = 0; i < N(flags); i++)
13121e06e4sam		if (strncasecmp(flags[i].name, name, len) == 0)
13221e06e4sam			return flags[i].bit;
13321e06e4sam	return 0;
13421e06e4sam}
13521e06e4sam
13621e06e4samstatic void
13721e06e4samusage(void)
13821e06e4sam{
13921e06e4sam	int i;
14021e06e4sam
14182036c4thompsa	fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname);
14221e06e4sam	fprintf(stderr, "where flags are:\n");
14321e06e4sam	for (i = 0; i < N(flags); i++)
14421e06e4sam		printf("%s\n", flags[i].name);
14521e06e4sam	exit(-1);
14621e06e4sam}
14721e06e4sam
148a56ef47samstatic void
149a56ef47samsetoid(char oid[], size_t oidlen, const char *wlan)
150a56ef47sam{
151a56ef47sam#ifdef __linux__
15282036c4thompsa	if (wlan)
15382036c4thompsa		snprintf(oid, oidlen, "net.%s.debug", wlan);
154a56ef47sam#elif __FreeBSD__
15582036c4thompsa	if (wlan)
15682036c4thompsa		snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4);
15782036c4thompsa	else
15882036c4thompsa		snprintf(oid, oidlen, "net.wlan.debug");
159a56ef47sam#elif __NetBSD__
16082036c4thompsa	if (wlan)
1611322406sam		snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan);
16282036c4thompsa	else
16382036c4thompsa		snprintf(oid, oidlen, "net.link.ieee80211.debug");
164a56ef47sam#else
165a56ef47sam#error "No support for this system"
166a56ef47sam#endif
167a56ef47sam}
168a56ef47sam
169b4413f0avosstatic void
170b4413f0avosget_orig_iface_name(char *oid, size_t oid_size, char *name)
171b4413f0avos{
172b4413f0avos	struct ifconfig_handle *h;
173b4413f0avos	char *orig_name;
174b4413f0avos
175b4413f0avos	h = ifconfig_open();
1763a29c96avos	if (ifconfig_get_orig_name(h, name, &orig_name) < 0) {
1773a29c96avos		/* check for original interface name. */
1783a29c96avos		orig_name = name;
1793a29c96avos	}
180b4413f0avos
1813a29c96avos	if (strlen(orig_name) < strlen("wlan") + 1 ||
1823a29c96avos	    strncmp(orig_name, "wlan", 4) != 0)
18391b6b9davos		errx(1, "expecting a wlan interface name");
18491b6b9davos
185b4413f0avos	ifconfig_close(h);
186b4413f0avos	setoid(oid, oid_size, orig_name);
1873a29c96avos	if (orig_name != name)
1883a29c96avos		free(orig_name);
189b4413f0avos}
190b4413f0avos
19121e06e4samint
19221e06e4sammain(int argc, char *argv[])
19321e06e4sam{
19421e06e4sam	const char *cp, *tp;
19521e06e4sam	const char *sep;
1961322406sam	int op, i;
19721e06e4sam	u_int32_t debug, ndebug;
1981322406sam	size_t debuglen;
199a56ef47sam	char oid[256];
20021e06e4sam
20121e06e4sam	progname = argv[0];
202a56ef47sam	setoid(oid, sizeof(oid), "wlan0");
20321e06e4sam	if (argc > 1) {
20482036c4thompsa		if (strcmp(argv[1], "-d") == 0) {
20582036c4thompsa			setoid(oid, sizeof(oid), NULL);
20682036c4thompsa			argc -= 1, argv += 1;
20782036c4thompsa		} else if (strcmp(argv[1], "-i") == 0) {
208b084c5dhiren			if (argc <= 2)
20921e06e4sam				errx(1, "missing interface name for -i option");
210b4413f0avos			get_orig_iface_name(oid, sizeof(oid), argv[2]);
21121e06e4sam			argc -= 2, argv += 2;
21221e06e4sam		} else if (strcmp(argv[1], "-?") == 0)
21321e06e4sam			usage();
21421e06e4sam	}
21521e06e4sam
21621e06e4sam	debuglen = sizeof(debug);
21721e06e4sam	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
21821e06e4sam		err(1, "sysctl-get(%s)", oid);
21921e06e4sam	ndebug = debug;
22021e06e4sam	for (; argc > 1; argc--, argv++) {
22121e06e4sam		cp = argv[1];
22221e06e4sam		do {
22321e06e4sam			u_int bit;
22421e06e4sam
22521e06e4sam			if (*cp == '-') {
22621e06e4sam				cp++;
22721e06e4sam				op = -1;
22821e06e4sam			} else if (*cp == '+') {
22921e06e4sam				cp++;
23021e06e4sam				op = 1;
23121e06e4sam			} else
23221e06e4sam				op = 0;
23321e06e4sam			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
23421e06e4sam				tp++;
23521e06e4sam			bit = getflag(cp, tp-cp);
23621e06e4sam			if (op < 0)
23721e06e4sam				ndebug &= ~bit;
23821e06e4sam			else if (op > 0)
23921e06e4sam				ndebug |= bit;
24021e06e4sam			else {
24121e06e4sam				if (bit == 0) {
242ffbf3d6sam					int c = *cp;
243ffbf3d6sam					if (isdigit(c))
24421e06e4sam						bit = strtoul(cp, NULL, 0);
24521e06e4sam					else
24621e06e4sam						errx(1, "unknown flag %.*s",
247ffbf3d6sam							(int)(tp-cp), cp);
24821e06e4sam				}
24921e06e4sam				ndebug = bit;
25021e06e4sam			}
25121e06e4sam		} while (*(cp = tp) != '\0');
25221e06e4sam	}
25321e06e4sam	if (debug != ndebug) {
25421e06e4sam		printf("%s: 0x%x => ", oid, debug);
25521e06e4sam		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
25621e06e4sam			err(1, "sysctl-set(%s)", oid);
25721e06e4sam		printf("0x%x", ndebug);
25821e06e4sam		debug = ndebug;
25921e06e4sam	} else
26021e06e4sam		printf("%s: 0x%x", oid, debug);
26121e06e4sam	sep = "<";
26221e06e4sam	for (i = 0; i < N(flags); i++)
26321e06e4sam		if (debug & flags[i].bit) {
26421e06e4sam			printf("%s%s", sep, flags[i].name);
26521e06e4sam			sep = ",";
26621e06e4sam		}
26721e06e4sam	printf("%s\n", *sep != '<' ? ">" : "");
26821e06e4sam	return 0;
26921e06e4sam}
270