Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / usr.sbin / wlandebug / wlandebug.c
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: src/usr.sbin/wlandebug/wlandebug.c,v 1.11 2009/07/17 21:11:08 sam Exp $
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 #define N(a)    (sizeof(a)/sizeof(a[0]))
47
48 const char *progname;
49
50 #define IEEE80211_MSG_11N       0x80000000      /* 11n mode debug */
51 #define IEEE80211_MSG_DEBUG     0x40000000      /* IFF_DEBUG equivalent */
52 #define IEEE80211_MSG_DUMPPKTS  0x20000000      /* IFF_LINK2 equivalant */
53 #define IEEE80211_MSG_CRYPTO    0x10000000      /* crypto work */
54 #define IEEE80211_MSG_INPUT     0x08000000      /* input handling */
55 #define IEEE80211_MSG_XRATE     0x04000000      /* rate set handling */
56 #define IEEE80211_MSG_ELEMID    0x02000000      /* element id parsing */
57 #define IEEE80211_MSG_NODE      0x01000000      /* node handling */
58 #define IEEE80211_MSG_ASSOC     0x00800000      /* association handling */
59 #define IEEE80211_MSG_AUTH      0x00400000      /* authentication handling */
60 #define IEEE80211_MSG_SCAN      0x00200000      /* scanning */
61 #define IEEE80211_MSG_OUTPUT    0x00100000      /* output handling */
62 #define IEEE80211_MSG_STATE     0x00080000      /* state machine */
63 #define IEEE80211_MSG_POWER     0x00040000      /* power save handling */
64 #define IEEE80211_MSG_HWMP      0x00020000      /* hybrid mesh protocol */
65 #define IEEE80211_MSG_DOT1XSM   0x00010000      /* 802.1x state machine */
66 #define IEEE80211_MSG_RADIUS    0x00008000      /* 802.1x radius client */
67 #define IEEE80211_MSG_RADDUMP   0x00004000      /* dump 802.1x radius packets */
68 #define IEEE80211_MSG_MESH      0x00002000      /* mesh networking */
69 #define IEEE80211_MSG_WPA       0x00001000      /* WPA/RSN protocol */
70 #define IEEE80211_MSG_ACL       0x00000800      /* ACL handling */
71 #define IEEE80211_MSG_WME       0x00000400      /* WME protocol */
72 #define IEEE80211_MSG_SUPERG    0x00000200      /* Atheros SuperG protocol */
73 #define IEEE80211_MSG_DOTH      0x00000100      /* 802.11h support */
74 #define IEEE80211_MSG_INACT     0x00000080      /* inactivity handling */
75 #define IEEE80211_MSG_ROAM      0x00000040      /* sta-mode roaming */
76 #define IEEE80211_MSG_RATECTL   0x00000020      /* tx rate control */
77 #define IEEE80211_MSG_ACTION    0x00000010      /* action frame handling */
78 #define IEEE80211_MSG_WDS       0x00000008      /* WDS handling */
79 #define IEEE80211_MSG_IOCTL     0x00000004      /* ioctl handling */
80 #define IEEE80211_MSG_TDMA      0x00000002      /* TDMA handling */
81
82 static struct {
83         const char      *name;
84         u_int           bit;
85 } flags[] = {
86         { "11n",        IEEE80211_MSG_11N },
87         { "debug",      IEEE80211_MSG_DEBUG },
88         { "dumppkts",   IEEE80211_MSG_DUMPPKTS },
89         { "crypto",     IEEE80211_MSG_CRYPTO },
90         { "input",      IEEE80211_MSG_INPUT },
91         { "xrate",      IEEE80211_MSG_XRATE },
92         { "elemid",     IEEE80211_MSG_ELEMID },
93         { "node",       IEEE80211_MSG_NODE },
94         { "assoc",      IEEE80211_MSG_ASSOC },
95         { "auth",       IEEE80211_MSG_AUTH },
96         { "scan",       IEEE80211_MSG_SCAN },
97         { "output",     IEEE80211_MSG_OUTPUT },
98         { "state",      IEEE80211_MSG_STATE },
99         { "power",      IEEE80211_MSG_POWER },
100         { "hwmp",       IEEE80211_MSG_HWMP },
101         { "dot1xsm",    IEEE80211_MSG_DOT1XSM },
102         { "radius",     IEEE80211_MSG_RADIUS },
103         { "raddump",    IEEE80211_MSG_RADDUMP },
104         { "mesh",       IEEE80211_MSG_MESH },
105         { "wpa",        IEEE80211_MSG_WPA },
106         { "acl",        IEEE80211_MSG_ACL },
107         { "wme",        IEEE80211_MSG_WME },
108         { "superg",     IEEE80211_MSG_SUPERG },
109         { "doth",       IEEE80211_MSG_DOTH },
110         { "inact",      IEEE80211_MSG_INACT },
111         { "roam",       IEEE80211_MSG_ROAM },
112         { "rate",       IEEE80211_MSG_RATECTL },
113         { "action",     IEEE80211_MSG_ACTION },
114         { "wds",        IEEE80211_MSG_WDS },
115         { "ioctl",      IEEE80211_MSG_IOCTL },
116         { "tdma",       IEEE80211_MSG_TDMA },
117 };
118
119 static u_int
120 getflag(const char *name, int len)
121 {
122         int i;
123
124         for (i = 0; i < N(flags); i++)
125                 if (strncasecmp(flags[i].name, name, len) == 0)
126                         return flags[i].bit;
127         return 0;
128 }
129
130 static void
131 usage(void)
132 {
133         int i;
134
135         fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname);
136         fprintf(stderr, "where flags are:\n");
137         for (i = 0; i < N(flags); i++)
138                 printf("%s\n", flags[i].name);
139         exit(-1);
140 }
141
142 static void
143 setoid(char oid[], size_t oidlen, const char *wlan)
144 {
145         if (wlan)
146                 snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4);
147         else
148                 snprintf(oid, oidlen, "net.wlan.debug");
149 }
150
151 int
152 main(int argc, char *argv[])
153 {
154         const char *cp, *tp;
155         const char *sep;
156         int op, i;
157         u_int32_t debug, ndebug;
158         size_t debuglen;
159         char oid[256];
160
161         progname = argv[0];
162         setoid(oid, sizeof(oid), "wlan0");
163         if (argc > 1) {
164                 if (strcmp(argv[1], "-d") == 0) {
165                         setoid(oid, sizeof(oid), NULL);
166                         argc -= 1, argv += 1;
167                 } else if (strcmp(argv[1], "-i") == 0) {
168                         if (argc < 2)
169                                 errx(1, "missing interface name for -i option");
170                         if (strncmp(argv[2], "wlan", 4) != 0)
171                                 errx(1, "expecting a wlan interface name");
172                         setoid(oid, sizeof(oid), argv[2]);
173                         argc -= 2, argv += 2;
174                 } else if (strcmp(argv[1], "-?") == 0)
175                         usage();
176         }
177
178         debuglen = sizeof(debug);
179         if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
180                 err(1, "sysctl-get(%s)", oid);
181         ndebug = debug;
182         for (; argc > 1; argc--, argv++) {
183                 cp = argv[1];
184                 do {
185                         u_int bit;
186
187                         if (*cp == '-') {
188                                 cp++;
189                                 op = -1;
190                         } else if (*cp == '+') {
191                                 cp++;
192                                 op = 1;
193                         } else
194                                 op = 0;
195                         for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
196                                 tp++;
197                         bit = getflag(cp, tp-cp);
198                         if (op < 0)
199                                 ndebug &= ~bit;
200                         else if (op > 0)
201                                 ndebug |= bit;
202                         else {
203                                 if (bit == 0) {
204                                         int c = *cp;
205                                         if (isdigit(c))
206                                                 bit = strtoul(cp, NULL, 0);
207                                         else
208                                                 errx(1, "unknown flag %.*s",
209                                                         (int)(tp-cp), cp);
210                                 }
211                                 ndebug = bit;
212                         }
213                 } while (*(cp = tp) != '\0');
214         }
215         if (debug != ndebug) {
216                 printf("%s: 0x%x => ", oid, debug);
217                 if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
218                         err(1, "sysctl-set(%s)", oid);
219                 printf("0x%x", ndebug);
220                 debug = ndebug;
221         } else
222                 printf("%s: 0x%x", oid, debug);
223         sep = "<";
224         for (i = 0; i < N(flags); i++)
225                 if (debug & flags[i].bit) {
226                         printf("%s%s", sep, flags[i].name);
227                         sep = ",";
228                 }
229         printf("%s\n", *sep != '<' ? ">" : "");
230         return 0;
231 }