wlandebug.c revision 195746
168583Smarcel/*-
268583Smarcel * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
368583Smarcel * All rights reserved.
468583Smarcel *
568583Smarcel * Redistribution and use in source and binary forms, with or without
6156843Snetchild * modification, are permitted provided that the following conditions
768583Smarcel * are met:
868583Smarcel * 1. Redistributions of source code must retain the above copyright
968583Smarcel *    notice, this list of conditions and the following disclaimer,
1068583Smarcel *    without modification.
1168583Smarcel * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1268583Smarcel *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
1368583Smarcel *    redistribution must be conditioned upon including a substantially
14122256Sjhb *    similar Disclaimer requirement for further binary redistribution.
15122256Sjhb *
16110977Stjr * NO WARRANTY
1768583Smarcel * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18122256Sjhb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19122256Sjhb * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
2068583Smarcel * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
2168583Smarcel * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
2283366Sjulian * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2383366Sjulian * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2468583Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2568583Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2668583Smarcel * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2783221Smarcel * THE POSSIBILITY OF SUCH DAMAGES.
2883221Smarcel *
2983221Smarcel * $FreeBSD: head/usr.sbin/wlandebug/wlandebug.c 195746 2009-07-17 21:11:08Z sam $
3083221Smarcel */
3183221Smarcel
3283221Smarcel/*
3383221Smarcel * wlandebug [-i interface] flags
3483221Smarcel * (default interface is wlan.0).
35143198Ssobomax */
3683221Smarcel#include <sys/types.h>
3768583Smarcel#include <sys/sysctl.h>
3868583Smarcel
3983221Smarcel#include <stdio.h>
4083221Smarcel#include <stdlib.h>
4183221Smarcel#include <ctype.h>
4283221Smarcel#include <getopt.h>
4368583Smarcel#include <string.h>
4483221Smarcel#include <err.h>
4583221Smarcel
4683221Smarcel#define	N(a)	(sizeof(a)/sizeof(a[0]))
4783221Smarcel
4868583Smarcelconst char *progname;
4983221Smarcel
5083221Smarcel#define	IEEE80211_MSG_11N	0x80000000	/* 11n mode debug */
5183221Smarcel#define	IEEE80211_MSG_DEBUG	0x40000000	/* IFF_DEBUG equivalent */
5268583Smarcel#define	IEEE80211_MSG_DUMPPKTS	0x20000000	/* IFF_LINK2 equivalant */
5383221Smarcel#define	IEEE80211_MSG_CRYPTO	0x10000000	/* crypto work */
5483221Smarcel#define	IEEE80211_MSG_INPUT	0x08000000	/* input handling */
5583221Smarcel#define	IEEE80211_MSG_XRATE	0x04000000	/* rate set handling */
5668583Smarcel#define	IEEE80211_MSG_ELEMID	0x02000000	/* element id parsing */
5783221Smarcel#define	IEEE80211_MSG_NODE	0x01000000	/* node handling */
5883221Smarcel#define	IEEE80211_MSG_ASSOC	0x00800000	/* association handling */
5968583Smarcel#define	IEEE80211_MSG_AUTH	0x00400000	/* authentication handling */
6083221Smarcel#define	IEEE80211_MSG_SCAN	0x00200000	/* scanning */
6183221Smarcel#define	IEEE80211_MSG_OUTPUT	0x00100000	/* output handling */
6283221Smarcel#define	IEEE80211_MSG_STATE	0x00080000	/* state machine */
6383221Smarcel#define	IEEE80211_MSG_POWER	0x00040000	/* power save handling */
6468583Smarcel#define	IEEE80211_MSG_HWMP	0x00020000	/* hybrid mesh protocol */
6583221Smarcel#define	IEEE80211_MSG_DOT1XSM	0x00010000	/* 802.1x state machine */
6683221Smarcel#define	IEEE80211_MSG_RADIUS	0x00008000	/* 802.1x radius client */
6768583Smarcel#define	IEEE80211_MSG_RADDUMP	0x00004000	/* dump 802.1x radius packets */
6883221Smarcel#define	IEEE80211_MSG_MESH	0x00002000	/* mesh networking */
6983221Smarcel#define	IEEE80211_MSG_WPA	0x00001000	/* WPA/RSN protocol */
7068583Smarcel#define	IEEE80211_MSG_ACL	0x00000800	/* ACL handling */
7183221Smarcel#define	IEEE80211_MSG_WME	0x00000400	/* WME protocol */
7283221Smarcel#define	IEEE80211_MSG_SUPERG	0x00000200	/* Atheros SuperG protocol */
7383221Smarcel#define	IEEE80211_MSG_DOTH	0x00000100	/* 802.11h support */
7483221Smarcel#define	IEEE80211_MSG_INACT	0x00000080	/* inactivity handling */
7568583Smarcel#define	IEEE80211_MSG_ROAM	0x00000040	/* sta-mode roaming */
7683221Smarcel#define	IEEE80211_MSG_RATECTL	0x00000020	/* tx rate control */
7783221Smarcel#define	IEEE80211_MSG_ACTION	0x00000010	/* action frame handling */
7883221Smarcel#define	IEEE80211_MSG_WDS	0x00000008	/* WDS handling */
7968583Smarcel#define	IEEE80211_MSG_IOCTL	0x00000004	/* ioctl handling */
8083221Smarcel#define	IEEE80211_MSG_TDMA	0x00000002	/* TDMA handling */
8183221Smarcel
8283221Smarcelstatic struct {
8383221Smarcel	const char	*name;
8468583Smarcel	u_int		bit;
8583221Smarcel} flags[] = {
8683221Smarcel	{ "11n",	IEEE80211_MSG_11N },
87156843Snetchild	{ "debug",	IEEE80211_MSG_DEBUG },
8868583Smarcel	{ "dumppkts",	IEEE80211_MSG_DUMPPKTS },
8983221Smarcel	{ "crypto",	IEEE80211_MSG_CRYPTO },
9083221Smarcel	{ "input",	IEEE80211_MSG_INPUT },
9183221Smarcel	{ "xrate",	IEEE80211_MSG_XRATE },
9283221Smarcel	{ "elemid",	IEEE80211_MSG_ELEMID },
9368583Smarcel	{ "node",	IEEE80211_MSG_NODE },
9483221Smarcel	{ "assoc",	IEEE80211_MSG_ASSOC },
9583221Smarcel	{ "auth",	IEEE80211_MSG_AUTH },
9668583Smarcel	{ "scan",	IEEE80211_MSG_SCAN },
9783221Smarcel	{ "output",	IEEE80211_MSG_OUTPUT },
9883221Smarcel	{ "state",	IEEE80211_MSG_STATE },
9983221Smarcel	{ "power",	IEEE80211_MSG_POWER },
10083221Smarcel	{ "hwmp",	IEEE80211_MSG_HWMP },
10183221Smarcel	{ "dot1xsm",	IEEE80211_MSG_DOT1XSM },
10283221Smarcel	{ "radius",	IEEE80211_MSG_RADIUS },
10368583Smarcel	{ "raddump",	IEEE80211_MSG_RADDUMP },
10483221Smarcel	{ "mesh",	IEEE80211_MSG_MESH },
10583221Smarcel	{ "wpa",	IEEE80211_MSG_WPA },
10668583Smarcel	{ "acl",	IEEE80211_MSG_ACL },
10783221Smarcel	{ "wme",	IEEE80211_MSG_WME },
10883221Smarcel	{ "superg",	IEEE80211_MSG_SUPERG },
10968583Smarcel	{ "doth",	IEEE80211_MSG_DOTH },
11083221Smarcel	{ "inact",	IEEE80211_MSG_INACT },
11168583Smarcel	{ "roam",	IEEE80211_MSG_ROAM },
11268583Smarcel	{ "rate",	IEEE80211_MSG_RATECTL },
11383221Smarcel	{ "action",	IEEE80211_MSG_ACTION },
11468583Smarcel	{ "wds",	IEEE80211_MSG_WDS },
11568583Smarcel	{ "ioctl",	IEEE80211_MSG_IOCTL },
11683221Smarcel	{ "tdma",	IEEE80211_MSG_TDMA },
11796889Smarcel};
11896889Smarcel
11996889Smarcelstatic u_int
12096889Smarcelgetflag(const char *name, int len)
12168583Smarcel{
12283221Smarcel	int i;
12383221Smarcel
12468583Smarcel	for (i = 0; i < N(flags); i++)
12583221Smarcel		if (strncasecmp(flags[i].name, name, len) == 0)
12683221Smarcel			return flags[i].bit;
127156843Snetchild	return 0;
12868583Smarcel}
12983221Smarcel
13068583Smarcelstatic void
13168583Smarcelusage(void)
13283221Smarcel{
13383221Smarcel	int i;
13483221Smarcel
13568583Smarcel	fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname);
13683221Smarcel	fprintf(stderr, "where flags are:\n");
13783221Smarcel	for (i = 0; i < N(flags); i++)
13883221Smarcel		printf("%s\n", flags[i].name);
13968583Smarcel	exit(-1);
14083221Smarcel}
14183221Smarcel
14268583Smarcelstatic void
14383221Smarcelsetoid(char oid[], size_t oidlen, const char *wlan)
14483221Smarcel{
14583221Smarcel#ifdef __linux__
14668583Smarcel	if (wlan)
14783221Smarcel		snprintf(oid, oidlen, "net.%s.debug", wlan);
14883221Smarcel#elif __FreeBSD__
14983221Smarcel	if (wlan)
15068583Smarcel		snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4);
15183221Smarcel	else
15283221Smarcel		snprintf(oid, oidlen, "net.wlan.debug");
15383221Smarcel#elif __NetBSD__
15468583Smarcel	if (wlan)
15583221Smarcel		snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan);
15683221Smarcel	else
15768583Smarcel		snprintf(oid, oidlen, "net.link.ieee80211.debug");
15883221Smarcel#else
15983221Smarcel#error "No support for this system"
16068583Smarcel#endif
16183221Smarcel}
16283221Smarcel
16368583Smarcelint
16483221Smarcelmain(int argc, char *argv[])
16583221Smarcel{
16668583Smarcel	const char *cp, *tp;
16783221Smarcel	const char *sep;
16883221Smarcel	int op, i;
16968583Smarcel	u_int32_t debug, ndebug;
17083221Smarcel	size_t debuglen;
17183221Smarcel	char oid[256];
17268583Smarcel
17383221Smarcel	progname = argv[0];
17483221Smarcel	setoid(oid, sizeof(oid), "wlan0");
17583221Smarcel	if (argc > 1) {
17668583Smarcel		if (strcmp(argv[1], "-d") == 0) {
17783221Smarcel			setoid(oid, sizeof(oid), NULL);
17868583Smarcel			argc -= 1, argv += 1;
17968583Smarcel		} else if (strcmp(argv[1], "-i") == 0) {
18083221Smarcel			if (argc < 2)
18183221Smarcel				errx(1, "missing interface name for -i option");
18268583Smarcel			if (strncmp(argv[2], "wlan", 4) != 0)
18383221Smarcel				errx(1, "expecting a wlan interface name");
18483221Smarcel			setoid(oid, sizeof(oid), argv[2]);
18583221Smarcel			argc -= 2, argv += 2;
18668583Smarcel		} else if (strcmp(argv[1], "-?") == 0)
18783221Smarcel			usage();
18883221Smarcel	}
18983221Smarcel
19083221Smarcel	debuglen = sizeof(debug);
19168583Smarcel	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
19283221Smarcel		err(1, "sysctl-get(%s)", oid);
19383221Smarcel	ndebug = debug;
19483221Smarcel	for (; argc > 1; argc--, argv++) {
19583221Smarcel		cp = argv[1];
19668583Smarcel		do {
19783221Smarcel			u_int bit;
19868583Smarcel
19968583Smarcel			if (*cp == '-') {
20083221Smarcel				cp++;
20183221Smarcel				op = -1;
20283221Smarcel			} else if (*cp == '+') {
20368583Smarcel				cp++;
20483221Smarcel				op = 1;
20583221Smarcel			} else
20683221Smarcel				op = 0;
20783221Smarcel			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
20868583Smarcel				tp++;
20983221Smarcel			bit = getflag(cp, tp-cp);
21068583Smarcel			if (op < 0)
21168583Smarcel				ndebug &= ~bit;
21283221Smarcel			else if (op > 0)
21383221Smarcel				ndebug |= bit;
21468583Smarcel			else {
21583221Smarcel				if (bit == 0) {
21683221Smarcel					int c = *cp;
21783221Smarcel					if (isdigit(c))
21868583Smarcel						bit = strtoul(cp, NULL, 0);
21983221Smarcel					else
22083221Smarcel						errx(1, "unknown flag %.*s",
22183221Smarcel							(int)(tp-cp), cp);
22268583Smarcel				}
22383221Smarcel				ndebug = bit;
22483221Smarcel			}
22583221Smarcel		} while (*(cp = tp) != '\0');
22683221Smarcel	}
22768583Smarcel	if (debug != ndebug) {
22883221Smarcel		printf("%s: 0x%x => ", oid, debug);
22983221Smarcel		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
23068583Smarcel			err(1, "sysctl-set(%s)", oid);
23183221Smarcel		printf("0x%x", ndebug);
23283221Smarcel		debug = ndebug;
23383221Smarcel	} else
23468583Smarcel		printf("%s: 0x%x", oid, debug);
23583221Smarcel	sep = "<";
23683221Smarcel	for (i = 0; i < N(flags); i++)
23783221Smarcel		if (debug & flags[i].bit) {
23868583Smarcel			printf("%s%s", sep, flags[i].name);
23983221Smarcel			sep = ",";
24083221Smarcel		}
24183221Smarcel	printf("%s\n", *sep != '<' ? ">" : "");
24268583Smarcel	return 0;
24383221Smarcel}
24483221Smarcel