1 /* $NetBSD: wlanctl.c,v 1.3 2005/11/20 09:41:39 dyoung Exp $ */ 2 /*- 3 * Copyright (c) 2005 David Young. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or 6 * without modification, are permitted provided that the following 7 * conditions are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following 12 * disclaimer in the documentation and/or other materials provided 13 * with the distribution. 14 * 3. The name of David Young may not be used to endorse or promote 15 * products derived from this software without specific prior 16 * written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David 22 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 29 * OF SUCH DAMAGE. 30 */ 31 #include <err.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <unistd.h> 36 37 #include <sys/param.h> 38 #include <sys/sysctl.h> 39 #include <sys/inttypes.h> 40 #include <sys/ioctl.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 44 #include <net/if.h> 45 #include <net/if_media.h> 46 #include <netinet/in.h> 47 #include <netinet/if_ether.h> 48 49 #include <net80211/ieee80211.h> 50 #include <net80211/ieee80211_node.h> 51 #include <net80211/ieee80211_sysctl.h> 52 53 #define NELTS(x) (sizeof(x)/sizeof((x)[0])) 54 55 struct flagname { 56 u_int32_t fn_flag; 57 const char *fn_name; 58 }; 59 60 struct cmdflags { 61 int cf_v; /* verbose */ 62 int cf_a; /* all 802.11 interfaces */ 63 }; 64 65 static void print_flags(u_int32_t, const struct flagname *, u_int); 66 static int dump_nodes(const char *, int, struct cmdflags *); 67 static const char *ether_string(u_int8_t *); 68 static void parse_args(int *, char ***, struct cmdflags *); 69 static void print_capinfo(u_int16_t); 70 static void print_channel(u_int16_t, u_int16_t, u_int16_t); 71 static void print_node_flags(u_int32_t); 72 static void print_rateset(struct ieee80211_rateset *, int); 73 static void usage(void); 74 75 static void 76 print_rateset(struct ieee80211_rateset *rs, int txrate) 77 { 78 int i, rate; 79 const char *fmt, *basic; 80 81 printf("\trates"); 82 83 for (i = 0; i < rs->rs_nrates; i++) { 84 85 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) != 0) 86 basic = "*"; 87 else 88 basic = ""; 89 if (i == txrate) 90 fmt = " [%s%d.%d]"; 91 else 92 fmt = " %s%d.%d"; 93 rate = 5 * (rs->rs_rates[i] & IEEE80211_RATE_VAL); 94 printf(fmt, basic, rate / 10, rate % 10); 95 } 96 printf("\n"); 97 } 98 99 static void 100 print_flags(u_int32_t flags, const struct flagname *flagnames, u_int nname) 101 { 102 int i; 103 const char *delim; 104 delim = "<"; 105 106 for (i = 0; i < nname; i++) { 107 if ((flags & flagnames[i].fn_flag) != 0) { 108 printf("%s%s", delim, flagnames[i].fn_name); 109 delim = ","; 110 } 111 } 112 113 printf("%s\n", (delim[0] == '<') ? "" : ">"); 114 } 115 116 static void 117 print_node_flags(u_int32_t flags) 118 { 119 const static struct flagname nodeflags[] = { 120 {IEEE80211_NODE_SYSCTL_F_BSS, "bss"} 121 , {IEEE80211_NODE_SYSCTL_F_STA, "sta"} 122 , {IEEE80211_NODE_SYSCTL_F_SCAN, "scan"} 123 }; 124 printf("\tnode flags %04x", flags); 125 126 print_flags(flags, nodeflags, NELTS(nodeflags)); 127 } 128 129 static void 130 print_capinfo(u_int16_t capinfo) 131 { 132 const static struct flagname capflags[] = { 133 {IEEE80211_CAPINFO_ESS, "ess"}, 134 {IEEE80211_CAPINFO_IBSS, "ibss"}, 135 {IEEE80211_CAPINFO_CF_POLLABLE, "cf pollable"}, 136 {IEEE80211_CAPINFO_CF_POLLREQ, "request cf poll"}, 137 {IEEE80211_CAPINFO_PRIVACY, "privacy"}, 138 {IEEE80211_CAPINFO_SHORT_PREAMBLE, "short preamble"}, 139 {IEEE80211_CAPINFO_PBCC, "pbcc"}, 140 {IEEE80211_CAPINFO_CHNL_AGILITY, "channel agility"}, 141 {IEEE80211_CAPINFO_SHORT_SLOTTIME, "short slot-time"}, 142 {IEEE80211_CAPINFO_RSN, "rsn"}, 143 {IEEE80211_CAPINFO_DSSSOFDM, "dsss-ofdm"} 144 }; 145 146 printf("\tcapabilities %04x", capinfo); 147 148 print_flags(capinfo, capflags, NELTS(capflags)); 149 } 150 151 static const char * 152 ether_string(u_int8_t *addr) 153 { 154 struct ether_addr ea; 155 (void)memcpy(ea.ether_addr_octet, addr, sizeof(ea.ether_addr_octet)); 156 return ether_ntoa(&ea); 157 } 158 159 static void 160 print_channel(u_int16_t chanidx, u_int16_t freq, u_int16_t flags) 161 { 162 const static struct flagname chanflags[] = { 163 {IEEE80211_CHAN_TURBO, "turbo"}, 164 {IEEE80211_CHAN_CCK, "cck"}, 165 {IEEE80211_CHAN_OFDM, "ofdm"}, 166 {IEEE80211_CHAN_2GHZ, "2.4GHz"}, 167 {IEEE80211_CHAN_5GHZ, "5GHz"}, 168 {IEEE80211_CHAN_PASSIVE, "passive scan"}, 169 {IEEE80211_CHAN_DYN, "dynamic cck-ofdm"}, 170 {IEEE80211_CHAN_GFSK, "gfsk"} 171 }; 172 printf("\tchan %d freq %dMHz flags %04x", chanidx, freq, flags); 173 174 print_flags(flags, chanflags, NELTS(chanflags)); 175 } 176 177 /* 178 * 179 * ifname: dump nodes belonging to the given interface, or belonging 180 * to all interfaces if NULL 181 * hdr_type: header type: IEEE80211_SYSCTL_T_NODE -> generic node, 182 * IEEE80211_SYSCTL_T_RSSADAPT -> rssadapt(9) info, 183 * IEEE80211_SYSCTL_T_DRVSPEC -> driver specific. 184 * cf: command flags, cf_v != 0 -> verbose 185 */ 186 static int 187 dump_nodes(const char *ifname_arg, int hdr_type, struct cmdflags *cf) 188 { 189 #if 0 190 /*39*/ u_int8_t ns_erp; /* 11g only */ 191 /*40*/ u_int32_t ns_rstamp; /* recv timestamp */ 192 /*64*/ u_int16_t ns_fhdwell; /* FH only */ 193 /*66*/ u_int8_t ns_fhindex; /* FH only */ 194 /*68*/ 195 #endif 196 u_int i, ifindex; 197 size_t namelen, nodes_len; 198 int name[10]; 199 int *vname; 200 char ifname[IFNAMSIZ]; 201 struct ieee80211_node_sysctl *pns, *ns; 202 203 namelen = NELTS(name); 204 205 if (sysctlnametomib("net.link.ieee80211.nodes", &name[0], 206 &namelen) != 0) { 207 warn("sysctlnametomib"); 208 return -1; 209 } 210 211 if (ifname_arg == NULL) 212 ifindex = 0; 213 else if ((ifindex = if_nametoindex(ifname_arg)) == 0) { 214 warn("if_nametoindex"); 215 return -1; 216 } 217 218 vname = &name[namelen]; 219 220 vname[IEEE80211_SYSCTL_NODENAME_IF] = ifindex; 221 vname[IEEE80211_SYSCTL_NODENAME_OP] = IEEE80211_SYSCTL_OP_ALL; 222 vname[IEEE80211_SYSCTL_NODENAME_ARG] = 0; 223 vname[IEEE80211_SYSCTL_NODENAME_TYPE] = hdr_type; 224 vname[IEEE80211_SYSCTL_NODENAME_ELTSIZE] = sizeof(*ns); 225 vname[IEEE80211_SYSCTL_NODENAME_ELTCOUNT] = INT_MAX; 226 227 /* how many? */ 228 if (sysctl(name, namelen + IEEE80211_SYSCTL_NODENAMELEN, 229 NULL, &nodes_len, NULL, 0) != 0) { 230 warn("sysctl(count)"); 231 return -1; 232 } 233 234 ns = (struct ieee80211_node_sysctl *)malloc(nodes_len); 235 236 if (ns == NULL) { 237 warn("malloc"); 238 return -1; 239 } 240 241 vname[IEEE80211_SYSCTL_NODENAME_ELTCOUNT] = nodes_len / sizeof(ns[0]); 242 243 /* Get them. */ 244 if (sysctl(name, namelen + IEEE80211_SYSCTL_NODENAMELEN, 245 ns, &nodes_len, NULL, 0) != 0) { 246 warn("sysctl(get)"); 247 return -1; 248 } 249 250 for (i = 0; i < nodes_len / sizeof(ns[0]); i++) { 251 pns = &ns[i]; 252 if (if_indextoname(pns->ns_ifindex, ifname) == NULL) { 253 warn("if_indextoname"); 254 return -1; 255 } 256 printf("%s: mac %s ", ifname, ether_string(pns->ns_macaddr)); 257 printf("bss %s\n", ether_string(pns->ns_bssid)); 258 print_node_flags(pns->ns_flags); 259 260 /* TBD deal with binary ESSID */ 261 printf("\tess <%.*s>\n", pns->ns_esslen, pns->ns_essid); 262 263 print_channel(pns->ns_chanidx, pns->ns_freq, pns->ns_chanflags); 264 265 print_capinfo(pns->ns_capinfo); 266 267 printf("\tbeacon-interval %d TU tsft %" PRIu64 " us\n", 268 pns->ns_intval, le64toh(*(u_int64_t *)&pns->ns_tstamp[0])); 269 270 print_rateset(&pns->ns_rates, pns->ns_txrate); 271 272 printf("\tassoc-id %d assoc-failed %d inactivity %ds\n", 273 pns->ns_associd, pns->ns_fails, pns->ns_inact); 274 275 printf("\trssi %d txseq %d rxseq %d\n", 276 pns->ns_rssi, pns->ns_txseq, pns->ns_rxseq); 277 } 278 return 0; 279 } 280 281 static void 282 usage(void) 283 { 284 fprintf(stderr, "usage: %s [ -v ] -a\n" 285 "\t[ -v ] interface [ interface ... ]\n", getprogname()); 286 exit(EXIT_FAILURE); 287 } 288 289 static void 290 parse_args(int *argcp, char ***argvp, struct cmdflags *cf) 291 { 292 int ch; 293 294 (void)memset(cf, 0, sizeof(*cf)); 295 296 while ((ch = getopt(*argcp, *argvp, "av")) != -1) { 297 switch (ch) { 298 case 'a': 299 cf->cf_a = 1; 300 break; 301 case 'v': 302 cf->cf_v = 1; 303 break; 304 default: 305 warnx("unknown option -%c", ch); 306 usage(); 307 } 308 } 309 310 *argcp -= optind; 311 *argvp += optind; 312 } 313 314 #define LOGICAL_XOR(x, y) (!(x) != !(y)) 315 316 int 317 main(int argc, char **argv) 318 { 319 int i; 320 struct cmdflags cf; 321 322 parse_args(&argc, &argv, &cf); 323 324 if (!LOGICAL_XOR(argc > 0, cf.cf_a)) 325 usage(); 326 327 if (cf.cf_a) { 328 if (dump_nodes(NULL, IEEE80211_SYSCTL_T_NODE, &cf) != 0) 329 return EXIT_FAILURE; 330 return EXIT_SUCCESS; 331 } 332 for (i = 0; i < argc; i++) { 333 if (dump_nodes(argv[i], IEEE80211_SYSCTL_T_NODE, &cf) != 0) 334 return EXIT_FAILURE; 335 } 336 337 return EXIT_SUCCESS; 338 } 339