ifieee80211.c revision 153892
1/* 2 * Copyright 2001 The Aerospace Corporation. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. The name of The Aerospace Corporation may not be used to endorse or 13 * promote products derived from this software. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sbin/ifconfig/ifieee80211.c 153892 2005-12-30 17:27:26Z rwatson $ 28 */ 29 30/*- 31 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc. 32 * All rights reserved. 33 * 34 * This code is derived from software contributed to The NetBSD Foundation 35 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 36 * NASA Ames Research Center. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the NetBSD 49 * Foundation, Inc. and its contributors. 50 * 4. Neither the name of The NetBSD Foundation nor the names of its 51 * contributors may be used to endorse or promote products derived 52 * from this software without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 55 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 64 * POSSIBILITY OF SUCH DAMAGE. 65 */ 66 67#include <sys/param.h> 68#include <sys/ioctl.h> 69#include <sys/socket.h> 70#include <sys/sysctl.h> 71#include <sys/time.h> 72 73#include <net/ethernet.h> 74#include <net/if.h> 75#include <net/if_dl.h> 76#include <net/if_types.h> 77#include <net/if_media.h> 78#include <net/route.h> 79 80#include <net80211/ieee80211.h> 81#include <net80211/ieee80211_crypto.h> 82#include <net80211/ieee80211_ioctl.h> 83 84#include <ctype.h> 85#include <err.h> 86#include <errno.h> 87#include <fcntl.h> 88#include <inttypes.h> 89#include <stdio.h> 90#include <stdlib.h> 91#include <string.h> 92#include <unistd.h> 93 94#include "ifconfig.h" 95 96static void set80211(int s, int type, int val, int len, u_int8_t *data); 97static const char *get_string(const char *val, const char *sep, 98 u_int8_t *buf, int *lenp); 99static void print_string(const u_int8_t *buf, int len); 100 101static int 102isanyarg(const char *arg) 103{ 104 return (strcmp(arg, "-") == 0 || 105 strcasecmp(arg, "any") == 0 || strcasecmp(arg, "off") == 0); 106} 107 108static void 109set80211ssid(const char *val, int d, int s, const struct afswtch *rafp) 110{ 111 int ssid; 112 int len; 113 u_int8_t data[IEEE80211_NWID_LEN]; 114 115 ssid = 0; 116 len = strlen(val); 117 if (len > 2 && isdigit(val[0]) && val[1] == ':') { 118 ssid = atoi(val)-1; 119 val += 2; 120 } 121 122 bzero(data, sizeof(data)); 123 len = sizeof(data); 124 if (get_string(val, NULL, data, &len) == NULL) 125 exit(1); 126 127 set80211(s, IEEE80211_IOC_SSID, ssid, len, data); 128} 129 130static void 131set80211stationname(const char *val, int d, int s, const struct afswtch *rafp) 132{ 133 int len; 134 u_int8_t data[33]; 135 136 bzero(data, sizeof(data)); 137 len = sizeof(data); 138 get_string(val, NULL, data, &len); 139 140 set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data); 141} 142 143/* 144 * Convert IEEE channel number to MHz frequency. 145 */ 146static u_int 147ieee80211_ieee2mhz(u_int chan) 148{ 149 if (chan == 14) 150 return 2484; 151 if (chan < 14) /* 0-13 */ 152 return 2407 + chan*5; 153 if (chan < 27) /* 15-26 */ 154 return 2512 + ((chan-15)*20); 155 return 5000 + (chan*5); 156} 157 158/* 159 * Convert MHz frequency to IEEE channel number. 160 */ 161static u_int 162ieee80211_mhz2ieee(u_int freq) 163{ 164 if (freq == 2484) 165 return 14; 166 if (freq < 2484) 167 return (freq - 2407) / 5; 168 if (freq < 5000) 169 return 15 + ((freq - 2512) / 20); 170 return (freq - 5000) / 5; 171} 172 173static void 174set80211channel(const char *val, int d, int s, const struct afswtch *rafp) 175{ 176 if (!isanyarg(val)) { 177 int v = atoi(val); 178 if (v > 255) /* treat as frequency */ 179 v = ieee80211_mhz2ieee(v); 180 set80211(s, IEEE80211_IOC_CHANNEL, v, 0, NULL); 181 } else 182 set80211(s, IEEE80211_IOC_CHANNEL, IEEE80211_CHAN_ANY, 0, NULL); 183} 184 185static void 186set80211authmode(const char *val, int d, int s, const struct afswtch *rafp) 187{ 188 int mode; 189 190 if (strcasecmp(val, "none") == 0) { 191 mode = IEEE80211_AUTH_NONE; 192 } else if (strcasecmp(val, "open") == 0) { 193 mode = IEEE80211_AUTH_OPEN; 194 } else if (strcasecmp(val, "shared") == 0) { 195 mode = IEEE80211_AUTH_SHARED; 196 } else if (strcasecmp(val, "8021x") == 0) { 197 mode = IEEE80211_AUTH_8021X; 198 } else if (strcasecmp(val, "wpa") == 0) { 199 mode = IEEE80211_AUTH_WPA; 200 } else { 201 errx(1, "unknown authmode"); 202 } 203 204 set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL); 205} 206 207static void 208set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp) 209{ 210 int mode; 211 212 if (strcasecmp(val, "off") == 0) { 213 mode = IEEE80211_POWERSAVE_OFF; 214 } else if (strcasecmp(val, "on") == 0) { 215 mode = IEEE80211_POWERSAVE_ON; 216 } else if (strcasecmp(val, "cam") == 0) { 217 mode = IEEE80211_POWERSAVE_CAM; 218 } else if (strcasecmp(val, "psp") == 0) { 219 mode = IEEE80211_POWERSAVE_PSP; 220 } else if (strcasecmp(val, "psp-cam") == 0) { 221 mode = IEEE80211_POWERSAVE_PSP_CAM; 222 } else { 223 errx(1, "unknown powersavemode"); 224 } 225 226 set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL); 227} 228 229static void 230set80211powersave(const char *val, int d, int s, const struct afswtch *rafp) 231{ 232 if (d == 0) 233 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF, 234 0, NULL); 235 else 236 set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON, 237 0, NULL); 238} 239 240static void 241set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp) 242{ 243 set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL); 244} 245 246static void 247set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp) 248{ 249 int mode; 250 251 if (strcasecmp(val, "off") == 0) { 252 mode = IEEE80211_WEP_OFF; 253 } else if (strcasecmp(val, "on") == 0) { 254 mode = IEEE80211_WEP_ON; 255 } else if (strcasecmp(val, "mixed") == 0) { 256 mode = IEEE80211_WEP_MIXED; 257 } else { 258 errx(1, "unknown wep mode"); 259 } 260 261 set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL); 262} 263 264static void 265set80211wep(const char *val, int d, int s, const struct afswtch *rafp) 266{ 267 set80211(s, IEEE80211_IOC_WEP, d, 0, NULL); 268} 269 270static int 271isundefarg(const char *arg) 272{ 273 return (strcmp(arg, "-") == 0 || strncasecmp(arg, "undef", 5) == 0); 274} 275 276static void 277set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp) 278{ 279 if (isundefarg(val)) 280 set80211(s, IEEE80211_IOC_WEPTXKEY, IEEE80211_KEYIX_NONE, 0, NULL); 281 else 282 set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL); 283} 284 285static void 286set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp) 287{ 288 int key = 0; 289 int len; 290 u_int8_t data[IEEE80211_KEYBUF_SIZE]; 291 292 if (isdigit(val[0]) && val[1] == ':') { 293 key = atoi(val)-1; 294 val += 2; 295 } 296 297 bzero(data, sizeof(data)); 298 len = sizeof(data); 299 get_string(val, NULL, data, &len); 300 301 set80211(s, IEEE80211_IOC_WEPKEY, key, len, data); 302} 303 304/* 305 * This function is purely a NetBSD compatability interface. The NetBSD 306 * interface is too inflexible, but it's there so we'll support it since 307 * it's not all that hard. 308 */ 309static void 310set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp) 311{ 312 int txkey; 313 int i, len; 314 u_int8_t data[IEEE80211_KEYBUF_SIZE]; 315 316 set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL); 317 318 if (isdigit(val[0]) && val[1] == ':') { 319 txkey = val[0]-'0'-1; 320 val += 2; 321 322 for (i = 0; i < 4; i++) { 323 bzero(data, sizeof(data)); 324 len = sizeof(data); 325 val = get_string(val, ",", data, &len); 326 if (val == NULL) 327 exit(1); 328 329 set80211(s, IEEE80211_IOC_WEPKEY, i, len, data); 330 } 331 } else { 332 bzero(data, sizeof(data)); 333 len = sizeof(data); 334 get_string(val, NULL, data, &len); 335 txkey = 0; 336 337 set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data); 338 339 bzero(data, sizeof(data)); 340 for (i = 1; i < 4; i++) 341 set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data); 342 } 343 344 set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL); 345} 346 347static void 348set80211rtsthreshold(const char *val, int d, int s, const struct afswtch *rafp) 349{ 350 set80211(s, IEEE80211_IOC_RTSTHRESHOLD, 351 isundefarg(val) ? IEEE80211_RTS_MAX : atoi(val), 0, NULL); 352} 353 354static void 355set80211protmode(const char *val, int d, int s, const struct afswtch *rafp) 356{ 357 int mode; 358 359 if (strcasecmp(val, "off") == 0) { 360 mode = IEEE80211_PROTMODE_OFF; 361 } else if (strcasecmp(val, "cts") == 0) { 362 mode = IEEE80211_PROTMODE_CTS; 363 } else if (strcasecmp(val, "rtscts") == 0) { 364 mode = IEEE80211_PROTMODE_RTSCTS; 365 } else { 366 errx(1, "unknown protection mode"); 367 } 368 369 set80211(s, IEEE80211_IOC_PROTMODE, mode, 0, NULL); 370} 371 372static void 373set80211txpower(const char *val, int d, int s, const struct afswtch *rafp) 374{ 375 set80211(s, IEEE80211_IOC_TXPOWER, atoi(val), 0, NULL); 376} 377 378#define IEEE80211_ROAMING_DEVICE 0 379#define IEEE80211_ROAMING_AUTO 1 380#define IEEE80211_ROAMING_MANUAL 2 381 382static void 383set80211roaming(const char *val, int d, int s, const struct afswtch *rafp) 384{ 385 int mode; 386 387 if (strcasecmp(val, "device") == 0) { 388 mode = IEEE80211_ROAMING_DEVICE; 389 } else if (strcasecmp(val, "auto") == 0) { 390 mode = IEEE80211_ROAMING_AUTO; 391 } else if (strcasecmp(val, "manual") == 0) { 392 mode = IEEE80211_ROAMING_MANUAL; 393 } else { 394 errx(1, "unknown roaming mode"); 395 } 396 set80211(s, IEEE80211_IOC_ROAMING, mode, 0, NULL); 397} 398 399static void 400set80211wme(const char *val, int d, int s, const struct afswtch *rafp) 401{ 402 set80211(s, IEEE80211_IOC_WME, d, 0, NULL); 403} 404 405static void 406set80211hidessid(const char *val, int d, int s, const struct afswtch *rafp) 407{ 408 set80211(s, IEEE80211_IOC_HIDESSID, d, 0, NULL); 409} 410 411static void 412set80211apbridge(const char *val, int d, int s, const struct afswtch *rafp) 413{ 414 set80211(s, IEEE80211_IOC_APBRIDGE, d, 0, NULL); 415} 416 417static void 418set80211chanlist(const char *val, int d, int s, const struct afswtch *rafp) 419{ 420 struct ieee80211req_chanlist chanlist; 421#define MAXCHAN (sizeof(chanlist.ic_channels)*NBBY) 422 char *temp, *cp, *tp; 423 424 temp = malloc(strlen(val) + 1); 425 if (temp == NULL) 426 errx(1, "malloc failed"); 427 strcpy(temp, val); 428 memset(&chanlist, 0, sizeof(chanlist)); 429 cp = temp; 430 for (;;) { 431 int first, last, f; 432 433 tp = strchr(cp, ','); 434 if (tp != NULL) 435 *tp++ = '\0'; 436 switch (sscanf(cp, "%u-%u", &first, &last)) { 437 case 1: 438 if (first > MAXCHAN) 439 errx(-1, "channel %u out of range, max %zu", 440 first, MAXCHAN); 441 setbit(chanlist.ic_channels, first); 442 break; 443 case 2: 444 if (first > MAXCHAN) 445 errx(-1, "channel %u out of range, max %zu", 446 first, MAXCHAN); 447 if (last > MAXCHAN) 448 errx(-1, "channel %u out of range, max %zu", 449 last, MAXCHAN); 450 if (first > last) 451 errx(-1, "void channel range, %u > %u", 452 first, last); 453 for (f = first; f <= last; f++) 454 setbit(chanlist.ic_channels, f); 455 break; 456 } 457 if (tp == NULL) 458 break; 459 while (isspace(*tp)) 460 tp++; 461 if (!isdigit(*tp)) 462 break; 463 cp = tp; 464 } 465 set80211(s, IEEE80211_IOC_CHANLIST, 0, 466 sizeof(chanlist), (uint8_t *) &chanlist); 467#undef MAXCHAN 468} 469 470static void 471set80211bssid(const char *val, int d, int s, const struct afswtch *rafp) 472{ 473 474 if (!isanyarg(val)) { 475 char *temp; 476 struct sockaddr_dl sdl; 477 478 temp = malloc(strlen(val) + 1); 479 if (temp == NULL) 480 errx(1, "malloc failed"); 481 temp[0] = ':'; 482 strcpy(temp + 1, val); 483 sdl.sdl_len = sizeof(sdl); 484 link_addr(temp, &sdl); 485 free(temp); 486 if (sdl.sdl_alen != IEEE80211_ADDR_LEN) 487 errx(1, "malformed link-level address"); 488 set80211(s, IEEE80211_IOC_BSSID, 0, 489 IEEE80211_ADDR_LEN, LLADDR(&sdl)); 490 } else { 491 uint8_t zerobssid[IEEE80211_ADDR_LEN]; 492 memset(zerobssid, 0, sizeof(zerobssid)); 493 set80211(s, IEEE80211_IOC_BSSID, 0, 494 IEEE80211_ADDR_LEN, zerobssid); 495 } 496} 497 498static int 499getac(const char *ac) 500{ 501 if (strcasecmp(ac, "ac_be") == 0 || strcasecmp(ac, "be") == 0) 502 return WME_AC_BE; 503 if (strcasecmp(ac, "ac_bk") == 0 || strcasecmp(ac, "bk") == 0) 504 return WME_AC_BK; 505 if (strcasecmp(ac, "ac_vi") == 0 || strcasecmp(ac, "vi") == 0) 506 return WME_AC_VI; 507 if (strcasecmp(ac, "ac_vo") == 0 || strcasecmp(ac, "vo") == 0) 508 return WME_AC_VO; 509 errx(1, "unknown wme access class %s", ac); 510} 511 512static 513DECL_CMD_FUNC2(set80211cwmin, ac, val) 514{ 515 set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), getac(ac), NULL); 516} 517 518static 519DECL_CMD_FUNC2(set80211cwmax, ac, val) 520{ 521 set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), getac(ac), NULL); 522} 523 524static 525DECL_CMD_FUNC2(set80211aifs, ac, val) 526{ 527 set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), getac(ac), NULL); 528} 529 530static 531DECL_CMD_FUNC2(set80211txoplimit, ac, val) 532{ 533 set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), getac(ac), NULL); 534} 535 536static 537DECL_CMD_FUNC(set80211acm, ac, d) 538{ 539 set80211(s, IEEE80211_IOC_WME_ACM, 1, getac(ac), NULL); 540} 541static 542DECL_CMD_FUNC(set80211noacm, ac, d) 543{ 544 set80211(s, IEEE80211_IOC_WME_ACM, 0, getac(ac), NULL); 545} 546 547static 548DECL_CMD_FUNC(set80211ackpolicy, ac, d) 549{ 550 set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 1, getac(ac), NULL); 551} 552static 553DECL_CMD_FUNC(set80211noackpolicy, ac, d) 554{ 555 set80211(s, IEEE80211_IOC_WME_ACKPOLICY, 0, getac(ac), NULL); 556} 557 558static 559DECL_CMD_FUNC2(set80211bsscwmin, ac, val) 560{ 561 set80211(s, IEEE80211_IOC_WME_CWMIN, atoi(val), 562 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL); 563} 564 565static 566DECL_CMD_FUNC2(set80211bsscwmax, ac, val) 567{ 568 set80211(s, IEEE80211_IOC_WME_CWMAX, atoi(val), 569 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL); 570} 571 572static 573DECL_CMD_FUNC2(set80211bssaifs, ac, val) 574{ 575 set80211(s, IEEE80211_IOC_WME_AIFS, atoi(val), 576 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL); 577} 578 579static 580DECL_CMD_FUNC2(set80211bsstxoplimit, ac, val) 581{ 582 set80211(s, IEEE80211_IOC_WME_TXOPLIMIT, atoi(val), 583 getac(ac)|IEEE80211_WMEPARAM_BSS, NULL); 584} 585 586static 587DECL_CMD_FUNC(set80211dtimperiod, val, d) 588{ 589 set80211(s, IEEE80211_IOC_DTIM_PERIOD, atoi(val), 0, NULL); 590} 591 592static 593DECL_CMD_FUNC(set80211bintval, val, d) 594{ 595 set80211(s, IEEE80211_IOC_BEACON_INTERVAL, atoi(val), 0, NULL); 596} 597 598static void 599set80211macmac(int s, int op, const char *val) 600{ 601 char *temp; 602 struct sockaddr_dl sdl; 603 604 temp = malloc(strlen(val) + 1); 605 if (temp == NULL) 606 errx(1, "malloc failed"); 607 temp[0] = ':'; 608 strcpy(temp + 1, val); 609 sdl.sdl_len = sizeof(sdl); 610 link_addr(temp, &sdl); 611 free(temp); 612 if (sdl.sdl_alen != IEEE80211_ADDR_LEN) 613 errx(1, "malformed link-level address"); 614 set80211(s, op, 0, IEEE80211_ADDR_LEN, LLADDR(&sdl)); 615} 616 617static 618DECL_CMD_FUNC(set80211addmac, val, d) 619{ 620 set80211macmac(s, IEEE80211_IOC_ADDMAC, val); 621} 622 623static 624DECL_CMD_FUNC(set80211delmac, val, d) 625{ 626 set80211macmac(s, IEEE80211_IOC_DELMAC, val); 627} 628 629static 630DECL_CMD_FUNC(set80211kickmac, val, d) 631{ 632 char *temp; 633 struct sockaddr_dl sdl; 634 struct ieee80211req_mlme mlme; 635 636 temp = malloc(strlen(val) + 1); 637 if (temp == NULL) 638 errx(1, "malloc failed"); 639 temp[0] = ':'; 640 strcpy(temp + 1, val); 641 sdl.sdl_len = sizeof(sdl); 642 link_addr(temp, &sdl); 643 free(temp); 644 if (sdl.sdl_alen != IEEE80211_ADDR_LEN) 645 errx(1, "malformed link-level address"); 646 memset(&mlme, 0, sizeof(mlme)); 647 mlme.im_op = IEEE80211_MLME_DEAUTH; 648 mlme.im_reason = IEEE80211_REASON_AUTH_EXPIRE; 649 memcpy(mlme.im_macaddr, LLADDR(&sdl), IEEE80211_ADDR_LEN); 650 set80211(s, IEEE80211_IOC_MLME, 0, sizeof(mlme), (u_int8_t *) &mlme); 651} 652 653static 654DECL_CMD_FUNC(set80211maccmd, val, d) 655{ 656 set80211(s, IEEE80211_IOC_MACCMD, d, 0, NULL); 657} 658 659static void 660set80211pureg(const char *val, int d, int s, const struct afswtch *rafp) 661{ 662 set80211(s, IEEE80211_IOC_PUREG, d, 0, NULL); 663} 664 665static void 666set80211burst(const char *val, int d, int s, const struct afswtch *rafp) 667{ 668 set80211(s, IEEE80211_IOC_BURST, d, 0, NULL); 669} 670 671static 672DECL_CMD_FUNC(set80211mcastrate, val, d) 673{ 674 set80211(s, IEEE80211_IOC_MCAST_RATE, (int) 2*atof(val), 0, NULL); 675} 676 677static 678DECL_CMD_FUNC(set80211fragthreshold, val, d) 679{ 680 set80211(s, IEEE80211_IOC_FRAGTHRESHOLD, 681 isundefarg(val) ? IEEE80211_FRAG_MAX : atoi(val), 0, NULL); 682} 683 684static int 685getmaxrate(uint8_t rates[15], uint8_t nrates) 686{ 687 int i, maxrate = -1; 688 689 for (i = 0; i < nrates; i++) { 690 int rate = rates[i] & IEEE80211_RATE_VAL; 691 if (rate > maxrate) 692 maxrate = rate; 693 } 694 return maxrate / 2; 695} 696 697static const char * 698getcaps(int capinfo) 699{ 700 static char capstring[32]; 701 char *cp = capstring; 702 703 if (capinfo & IEEE80211_CAPINFO_ESS) 704 *cp++ = 'E'; 705 if (capinfo & IEEE80211_CAPINFO_IBSS) 706 *cp++ = 'I'; 707 if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE) 708 *cp++ = 'c'; 709 if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ) 710 *cp++ = 'C'; 711 if (capinfo & IEEE80211_CAPINFO_PRIVACY) 712 *cp++ = 'P'; 713 if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) 714 *cp++ = 'S'; 715 if (capinfo & IEEE80211_CAPINFO_PBCC) 716 *cp++ = 'B'; 717 if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY) 718 *cp++ = 'A'; 719 if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) 720 *cp++ = 's'; 721 if (capinfo & IEEE80211_CAPINFO_RSN) 722 *cp++ = 'R'; 723 if (capinfo & IEEE80211_CAPINFO_DSSSOFDM) 724 *cp++ = 'D'; 725 *cp = '\0'; 726 return capstring; 727} 728 729static void 730printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen) 731{ 732 printf("%s", tag); 733 if (verbose) { 734 maxlen -= strlen(tag)+2; 735 if (2*ielen > maxlen) 736 maxlen--; 737 printf("<"); 738 for (; ielen > 0; ie++, ielen--) { 739 if (maxlen-- <= 0) 740 break; 741 printf("%02x", *ie); 742 } 743 if (ielen != 0) 744 printf("-"); 745 printf(">"); 746 } 747} 748 749/* 750 * Copy the ssid string contents into buf, truncating to fit. If the 751 * ssid is entirely printable then just copy intact. Otherwise convert 752 * to hexadecimal. If the result is truncated then replace the last 753 * three characters with "...". 754 */ 755static int 756copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len) 757{ 758 const u_int8_t *p; 759 size_t maxlen; 760 int i; 761 762 if (essid_len > bufsize) 763 maxlen = bufsize; 764 else 765 maxlen = essid_len; 766 /* determine printable or not */ 767 for (i = 0, p = essid; i < maxlen; i++, p++) { 768 if (*p < ' ' || *p > 0x7e) 769 break; 770 } 771 if (i != maxlen) { /* not printable, print as hex */ 772 if (bufsize < 3) 773 return 0; 774 strlcpy(buf, "0x", bufsize); 775 bufsize -= 2; 776 p = essid; 777 for (i = 0; i < maxlen && bufsize >= 2; i++) { 778 sprintf(&buf[2+2*i], "%02x", p[i]); 779 bufsize -= 2; 780 } 781 if (i != essid_len) 782 memcpy(&buf[2+2*i-3], "...", 3); 783 } else { /* printable, truncate as needed */ 784 memcpy(buf, essid, maxlen); 785 if (maxlen != essid_len) 786 memcpy(&buf[maxlen-3], "...", 3); 787 } 788 return maxlen; 789} 790 791/* unaligned little endian access */ 792#define LE_READ_4(p) \ 793 ((u_int32_t) \ 794 ((((const u_int8_t *)(p))[0] ) | \ 795 (((const u_int8_t *)(p))[1] << 8) | \ 796 (((const u_int8_t *)(p))[2] << 16) | \ 797 (((const u_int8_t *)(p))[3] << 24))) 798 799static int __inline 800iswpaoui(const u_int8_t *frm) 801{ 802 return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI); 803} 804 805static int __inline 806iswmeoui(const u_int8_t *frm) 807{ 808 return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI); 809} 810 811static int __inline 812isatherosoui(const u_int8_t *frm) 813{ 814 return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI); 815} 816 817static void 818printies(const u_int8_t *vp, int ielen, int maxcols) 819{ 820 while (ielen > 0) { 821 switch (vp[0]) { 822 case IEEE80211_ELEMID_VENDOR: 823 if (iswpaoui(vp)) 824 printie(" WPA", vp, 2+vp[1], maxcols); 825 else if (iswmeoui(vp)) 826 printie(" WME", vp, 2+vp[1], maxcols); 827 else if (isatherosoui(vp)) 828 printie(" ATH", vp, 2+vp[1], maxcols); 829 else 830 printie(" VEN", vp, 2+vp[1], maxcols); 831 break; 832 case IEEE80211_ELEMID_RSN: 833 printie(" RSN", vp, 2+vp[1], maxcols); 834 break; 835 default: 836 printie(" ???", vp, 2+vp[1], maxcols); 837 break; 838 } 839 ielen -= 2+vp[1]; 840 vp += 2+vp[1]; 841 } 842} 843 844static void 845list_scan(int s) 846{ 847 uint8_t buf[24*1024]; 848 struct ieee80211req ireq; 849 char ssid[IEEE80211_NWID_LEN+1]; 850 uint8_t *cp; 851 int len; 852 853 (void) memset(&ireq, 0, sizeof(ireq)); 854 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 855 ireq.i_type = IEEE80211_IOC_SCAN_RESULTS; 856 ireq.i_data = buf; 857 ireq.i_len = sizeof(buf); 858 if (ioctl(s, SIOCG80211, &ireq) < 0) 859 errx(1, "unable to get scan results"); 860 len = ireq.i_len; 861 if (len < sizeof(struct ieee80211req_scan_result)) 862 return; 863 864 printf("%-32.32s %-17.17s %4s %4s %-5s %3s %4s\n" 865 , "SSID" 866 , "BSSID" 867 , "CHAN" 868 , "RATE" 869 , "S:N" 870 , "INT" 871 , "CAPS" 872 ); 873 cp = buf; 874 do { 875 struct ieee80211req_scan_result *sr; 876 uint8_t *vp; 877 878 sr = (struct ieee80211req_scan_result *) cp; 879 vp = (u_int8_t *)(sr+1); 880 printf("%-32.*s %s %3d %3dM %2d:%-2d %3d %-4.4s" 881 , copy_essid(ssid, sizeof(ssid), vp, sr->isr_ssid_len) 882 , ssid 883 , ether_ntoa((const struct ether_addr *) sr->isr_bssid) 884 , ieee80211_mhz2ieee(sr->isr_freq) 885 , getmaxrate(sr->isr_rates, sr->isr_nrates) 886 , sr->isr_rssi, sr->isr_noise 887 , sr->isr_intval 888 , getcaps(sr->isr_capinfo) 889 ); 890 printies(vp + sr->isr_ssid_len, sr->isr_ie_len, 24);; 891 printf("\n"); 892 cp += sr->isr_len, len -= sr->isr_len; 893 } while (len >= sizeof(struct ieee80211req_scan_result)); 894} 895 896#include <net80211/ieee80211_freebsd.h> 897 898static void 899scan_and_wait(int s) 900{ 901 struct ieee80211req ireq; 902 int sroute; 903 904 sroute = socket(PF_ROUTE, SOCK_RAW, 0); 905 if (sroute < 0) { 906 perror("socket(PF_ROUTE,SOCK_RAW)"); 907 return; 908 } 909 (void) memset(&ireq, 0, sizeof(ireq)); 910 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 911 ireq.i_type = IEEE80211_IOC_SCAN_REQ; 912 /* NB: only root can trigger a scan so ignore errors */ 913 if (ioctl(s, SIOCS80211, &ireq) >= 0) { 914 char buf[2048]; 915 struct if_announcemsghdr *ifan; 916 struct rt_msghdr *rtm; 917 918 do { 919 if (read(sroute, buf, sizeof(buf)) < 0) { 920 perror("read(PF_ROUTE)"); 921 break; 922 } 923 rtm = (struct rt_msghdr *) buf; 924 if (rtm->rtm_version != RTM_VERSION) 925 break; 926 ifan = (struct if_announcemsghdr *) rtm; 927 } while (rtm->rtm_type != RTM_IEEE80211 || 928 ifan->ifan_what != RTM_IEEE80211_SCAN); 929 } 930 close(sroute); 931} 932 933static 934DECL_CMD_FUNC(set80211scan, val, d) 935{ 936 scan_and_wait(s); 937 list_scan(s); 938} 939 940static void 941list_stations(int s) 942{ 943 uint8_t buf[24*1024]; 944 struct ieee80211req ireq; 945 uint8_t *cp; 946 int len; 947 948 (void) memset(&ireq, 0, sizeof(ireq)); 949 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 950 ireq.i_type = IEEE80211_IOC_STA_INFO; 951 ireq.i_data = buf; 952 ireq.i_len = sizeof(buf); 953 if (ioctl(s, SIOCG80211, &ireq) < 0) 954 errx(1, "unable to get station information"); 955 len = ireq.i_len; 956 if (len < sizeof(struct ieee80211req_sta_info)) 957 return; 958 959 printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %3s\n" 960 , "ADDR" 961 , "AID" 962 , "CHAN" 963 , "RATE" 964 , "RSSI" 965 , "IDLE" 966 , "TXSEQ" 967 , "RXSEQ" 968 , "CAPS" 969 , "ERP" 970 ); 971 cp = buf; 972 do { 973 struct ieee80211req_sta_info *si; 974 uint8_t *vp; 975 976 si = (struct ieee80211req_sta_info *) cp; 977 vp = (u_int8_t *)(si+1); 978 printf("%s %4u %4d %3dM %4d %4d %6d %6d %-4.4s %3x" 979 , ether_ntoa((const struct ether_addr*) si->isi_macaddr) 980 , IEEE80211_AID(si->isi_associd) 981 , ieee80211_mhz2ieee(si->isi_freq) 982 , (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL)/2 983 , si->isi_rssi 984 , si->isi_inact 985 , si->isi_txseqs[0] 986 , si->isi_rxseqs[0] 987 , getcaps(si->isi_capinfo) 988 , si->isi_erp 989 ); 990 printies(vp, si->isi_ie_len, 24); 991 printf("\n"); 992 cp += si->isi_len, len -= si->isi_len; 993 } while (len >= sizeof(struct ieee80211req_sta_info)); 994} 995 996static void 997print_chaninfo(const struct ieee80211_channel *c) 998{ 999#define IEEE80211_IS_CHAN_PASSIVE(_c) \ 1000 (((_c)->ic_flags & IEEE80211_CHAN_PASSIVE)) 1001 char buf[14]; 1002 1003 buf[0] = '\0'; 1004 if (IEEE80211_IS_CHAN_FHSS(c)) 1005 strlcat(buf, " FHSS", sizeof(buf)); 1006 if (IEEE80211_IS_CHAN_A(c)) 1007 strlcat(buf, " 11a", sizeof(buf)); 1008 /* XXX 11g schizophrenia */ 1009 if (IEEE80211_IS_CHAN_G(c) || 1010 IEEE80211_IS_CHAN_PUREG(c)) 1011 strlcat(buf, " 11g", sizeof(buf)); 1012 else if (IEEE80211_IS_CHAN_B(c)) 1013 strlcat(buf, " 11b", sizeof(buf)); 1014 if (IEEE80211_IS_CHAN_T(c)) 1015 strlcat(buf, " Turbo", sizeof(buf)); 1016 printf("Channel %3u : %u%c Mhz%-14.14s", 1017 ieee80211_mhz2ieee(c->ic_freq), c->ic_freq, 1018 IEEE80211_IS_CHAN_PASSIVE(c) ? '*' : ' ', buf); 1019#undef IEEE80211_IS_CHAN_PASSIVE 1020} 1021 1022static void 1023list_channels(int s, int allchans) 1024{ 1025 struct ieee80211req ireq; 1026 struct ieee80211req_chaninfo chans; 1027 struct ieee80211req_chaninfo achans; 1028 const struct ieee80211_channel *c; 1029 int i, half; 1030 1031 (void) memset(&ireq, 0, sizeof(ireq)); 1032 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 1033 ireq.i_type = IEEE80211_IOC_CHANINFO; 1034 ireq.i_data = &chans; 1035 ireq.i_len = sizeof(chans); 1036 if (ioctl(s, SIOCG80211, &ireq) < 0) 1037 errx(1, "unable to get channel information"); 1038 if (!allchans) { 1039 struct ieee80211req_chanlist active; 1040 1041 ireq.i_type = IEEE80211_IOC_CHANLIST; 1042 ireq.i_data = &active; 1043 ireq.i_len = sizeof(active); 1044 if (ioctl(s, SIOCG80211, &ireq) < 0) 1045 errx(1, "unable to get active channel list"); 1046 memset(&achans, 0, sizeof(achans)); 1047 for (i = 0; i < chans.ic_nchans; i++) { 1048 c = &chans.ic_chans[i]; 1049 if (isset(active.ic_channels, ieee80211_mhz2ieee(c->ic_freq)) || allchans) 1050 achans.ic_chans[achans.ic_nchans++] = *c; 1051 } 1052 } else 1053 achans = chans; 1054 half = achans.ic_nchans / 2; 1055 if (achans.ic_nchans % 2) 1056 half++; 1057 for (i = 0; i < achans.ic_nchans / 2; i++) { 1058 print_chaninfo(&achans.ic_chans[i]); 1059 print_chaninfo(&achans.ic_chans[half+i]); 1060 printf("\n"); 1061 } 1062 if (achans.ic_nchans % 2) { 1063 print_chaninfo(&achans.ic_chans[i]); 1064 printf("\n"); 1065 } 1066} 1067 1068static void 1069list_keys(int s) 1070{ 1071} 1072 1073#define IEEE80211_C_BITS \ 1074"\020\1WEP\2TKIP\3AES\4AES_CCM\6CKIP\11IBSS\12PMGT\13HOSTAP\14AHDEMO" \ 1075"\15SWRETRY\16TXPMGT\17SHSLOT\20SHPREAMBLE\21MONITOR\22TKIPMIC\30WPA1" \ 1076"\31WPA2\32BURST\33WME" 1077 1078static void 1079list_capabilities(int s) 1080{ 1081 struct ieee80211req ireq; 1082 u_int32_t caps; 1083 1084 (void) memset(&ireq, 0, sizeof(ireq)); 1085 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 1086 ireq.i_type = IEEE80211_IOC_DRIVER_CAPS; 1087 if (ioctl(s, SIOCG80211, &ireq) < 0) 1088 errx(1, "unable to get driver capabilities"); 1089 caps = (((u_int16_t) ireq.i_val) << 16) | ((u_int16_t) ireq.i_len); 1090 printb(name, caps, IEEE80211_C_BITS); 1091 putchar('\n'); 1092} 1093 1094static void 1095list_wme(int s) 1096{ 1097 static const char *acnames[] = { "AC_BE", "AC_BK", "AC_VI", "AC_VO" }; 1098 struct ieee80211req ireq; 1099 int ac; 1100 1101 (void) memset(&ireq, 0, sizeof(ireq)); 1102 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 1103 ireq.i_len = 0; 1104 for (ac = WME_AC_BE; ac <= WME_AC_VO; ac++) { 1105again: 1106 if (ireq.i_len & IEEE80211_WMEPARAM_BSS) 1107 printf("\t%s", " "); 1108 else 1109 printf("\t%s", acnames[ac]); 1110 1111 ireq.i_len = (ireq.i_len & IEEE80211_WMEPARAM_BSS) | ac; 1112 1113 /* show WME BSS parameters */ 1114 ireq.i_type = IEEE80211_IOC_WME_CWMIN; 1115 if (ioctl(s, SIOCG80211, &ireq) != -1) 1116 printf(" cwmin %2u", ireq.i_val); 1117 ireq.i_type = IEEE80211_IOC_WME_CWMAX; 1118 if (ioctl(s, SIOCG80211, &ireq) != -1) 1119 printf(" cwmax %2u", ireq.i_val); 1120 ireq.i_type = IEEE80211_IOC_WME_AIFS; 1121 if (ioctl(s, SIOCG80211, &ireq) != -1) 1122 printf(" aifs %2u", ireq.i_val); 1123 ireq.i_type = IEEE80211_IOC_WME_TXOPLIMIT; 1124 if (ioctl(s, SIOCG80211, &ireq) != -1) 1125 printf(" txopLimit %3u", ireq.i_val); 1126 ireq.i_type = IEEE80211_IOC_WME_ACM; 1127 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1128 if (ireq.i_val) 1129 printf(" acm"); 1130 else if (verbose) 1131 printf(" -acm"); 1132 } 1133 /* !BSS only */ 1134 if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) { 1135 ireq.i_type = IEEE80211_IOC_WME_ACKPOLICY; 1136 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1137 if (!ireq.i_val) 1138 printf(" -ack"); 1139 else if (verbose) 1140 printf(" ack"); 1141 } 1142 } 1143 printf("\n"); 1144 if ((ireq.i_len & IEEE80211_WMEPARAM_BSS) == 0) { 1145 ireq.i_len |= IEEE80211_WMEPARAM_BSS; 1146 goto again; 1147 } else 1148 ireq.i_len &= ~IEEE80211_WMEPARAM_BSS; 1149 } 1150} 1151 1152static void 1153list_mac(int s) 1154{ 1155 struct ieee80211req ireq; 1156 struct ieee80211req_maclist *acllist; 1157 int i, nacls, policy; 1158 char c; 1159 1160 (void) memset(&ireq, 0, sizeof(ireq)); 1161 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); /* XXX ?? */ 1162 ireq.i_type = IEEE80211_IOC_MACCMD; 1163 ireq.i_val = IEEE80211_MACCMD_POLICY; 1164 if (ioctl(s, SIOCG80211, &ireq) < 0) { 1165 if (errno == EINVAL) { 1166 printf("No acl policy loaded\n"); 1167 return; 1168 } 1169 err(1, "unable to get mac policy"); 1170 } 1171 policy = ireq.i_val; 1172 1173 ireq.i_val = IEEE80211_MACCMD_LIST; 1174 ireq.i_len = 0; 1175 if (ioctl(s, SIOCG80211, &ireq) < 0) 1176 err(1, "unable to get mac acl list size"); 1177 if (ireq.i_len == 0) /* NB: no acls */ 1178 return; 1179 1180 ireq.i_data = malloc(ireq.i_len); 1181 if (ireq.i_data == NULL) 1182 err(1, "out of memory for acl list"); 1183 1184 if (ioctl(s, SIOCG80211, &ireq) < 0) 1185 err(1, "unable to get mac acl list"); 1186 if (policy == IEEE80211_MACCMD_POLICY_OPEN) { 1187 if (verbose) 1188 printf("policy: open\n"); 1189 c = '*'; 1190 } else if (policy == IEEE80211_MACCMD_POLICY_ALLOW) { 1191 if (verbose) 1192 printf("policy: allow\n"); 1193 c = '+'; 1194 } else if (policy == IEEE80211_MACCMD_POLICY_DENY) { 1195 if (verbose) 1196 printf("policy: deny\n"); 1197 c = '-'; 1198 } else { 1199 printf("policy: unknown (%u)\n", policy); 1200 c = '?'; 1201 } 1202 nacls = ireq.i_len / sizeof(*acllist); 1203 acllist = (struct ieee80211req_maclist *) ireq.i_data; 1204 for (i = 0; i < nacls; i++) 1205 printf("%c%s\n", c, ether_ntoa( 1206 (const struct ether_addr *) acllist[i].ml_macaddr)); 1207} 1208 1209static 1210DECL_CMD_FUNC(set80211list, arg, d) 1211{ 1212#define iseq(a,b) (strncasecmp(a,b,sizeof(b)-1) == 0) 1213 1214 if (iseq(arg, "sta")) 1215 list_stations(s); 1216 else if (iseq(arg, "scan") || iseq(arg, "ap")) 1217 list_scan(s); 1218 else if (iseq(arg, "chan") || iseq(arg, "freq")) 1219 list_channels(s, 1); 1220 else if (iseq(arg, "active")) 1221 list_channels(s, 0); 1222 else if (iseq(arg, "keys")) 1223 list_keys(s); 1224 else if (iseq(arg, "caps")) 1225 list_capabilities(s); 1226 else if (iseq(arg, "wme")) 1227 list_wme(s); 1228 else if (iseq(arg, "mac")) 1229 list_mac(s); 1230 else 1231 errx(1, "Don't know how to list %s for %s", arg, name); 1232#undef iseq 1233} 1234 1235static enum ieee80211_opmode 1236get80211opmode(int s) 1237{ 1238 struct ifmediareq ifmr; 1239 1240 (void) memset(&ifmr, 0, sizeof(ifmr)); 1241 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); 1242 1243 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) { 1244 if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) 1245 return IEEE80211_M_IBSS; /* XXX ahdemo */ 1246 if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP) 1247 return IEEE80211_M_HOSTAP; 1248 if (ifmr.ifm_current & IFM_IEEE80211_MONITOR) 1249 return IEEE80211_M_MONITOR; 1250 } 1251 return IEEE80211_M_STA; 1252} 1253 1254static const struct ieee80211_channel * 1255getchaninfo(int s, int chan) 1256{ 1257 struct ieee80211req ireq; 1258 static struct ieee80211req_chaninfo chans; 1259 static struct ieee80211_channel undef; 1260 const struct ieee80211_channel *c; 1261 int i, freq; 1262 1263 (void) memset(&ireq, 0, sizeof(ireq)); 1264 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 1265 ireq.i_type = IEEE80211_IOC_CHANINFO; 1266 ireq.i_data = &chans; 1267 ireq.i_len = sizeof(chans); 1268 if (ioctl(s, SIOCG80211, &ireq) < 0) 1269 errx(1, "unable to get channel information"); 1270 freq = ieee80211_ieee2mhz(chan); 1271 for (i = 0; i < chans.ic_nchans; i++) { 1272 c = &chans.ic_chans[i]; 1273 if (c->ic_freq == freq) 1274 return c; 1275 } 1276 return &undef; 1277} 1278 1279#if 0 1280static void 1281printcipher(int s, struct ieee80211req *ireq, int keylenop) 1282{ 1283 switch (ireq->i_val) { 1284 case IEEE80211_CIPHER_WEP: 1285 ireq->i_type = keylenop; 1286 if (ioctl(s, SIOCG80211, ireq) != -1) 1287 printf("WEP-%s", 1288 ireq->i_len <= 5 ? "40" : 1289 ireq->i_len <= 13 ? "104" : "128"); 1290 else 1291 printf("WEP"); 1292 break; 1293 case IEEE80211_CIPHER_TKIP: 1294 printf("TKIP"); 1295 break; 1296 case IEEE80211_CIPHER_AES_OCB: 1297 printf("AES-OCB"); 1298 break; 1299 case IEEE80211_CIPHER_AES_CCM: 1300 printf("AES-CCM"); 1301 break; 1302 case IEEE80211_CIPHER_CKIP: 1303 printf("CKIP"); 1304 break; 1305 case IEEE80211_CIPHER_NONE: 1306 printf("NONE"); 1307 break; 1308 default: 1309 printf("UNKNOWN (0x%x)", ireq->i_val); 1310 break; 1311 } 1312} 1313#endif 1314 1315#define MAXCOL 78 1316int col; 1317char spacer; 1318 1319#define LINE_BREAK() do { \ 1320 if (spacer != '\t') { \ 1321 printf("\n"); \ 1322 spacer = '\t'; \ 1323 } \ 1324 col = 8; /* 8-col tab */ \ 1325} while (0) 1326#define LINE_CHECK(fmt, ...) do { \ 1327 col += sizeof(fmt)-2; \ 1328 if (col > MAXCOL) { \ 1329 LINE_BREAK(); \ 1330 col += sizeof(fmt)-2; \ 1331 } \ 1332 printf(fmt, __VA_ARGS__); \ 1333 spacer = ' '; \ 1334} while (0) 1335 1336static void 1337printkey(const struct ieee80211req_key *ik) 1338{ 1339 static const uint8_t zerodata[IEEE80211_KEYBUF_SIZE]; 1340 int keylen = ik->ik_keylen; 1341 int printcontents; 1342 1343 printcontents = printkeys && 1344 (memcmp(ik->ik_keydata, zerodata, keylen) != 0 || verbose); 1345 if (printcontents) 1346 LINE_BREAK(); 1347 switch (ik->ik_type) { 1348 case IEEE80211_CIPHER_WEP: 1349 /* compatibility */ 1350 LINE_CHECK("%cwepkey %u:%s", spacer, ik->ik_keyix+1, 1351 keylen <= 5 ? "40-bit" : 1352 keylen <= 13 ? "104-bit" : "128-bit"); 1353 break; 1354 case IEEE80211_CIPHER_TKIP: 1355 if (keylen > 128/8) 1356 keylen -= 128/8; /* ignore MIC for now */ 1357 LINE_CHECK("%cTKIP %u:%u-bit", 1358 spacer, ik->ik_keyix+1, 8*keylen); 1359 break; 1360 case IEEE80211_CIPHER_AES_OCB: 1361 LINE_CHECK("%cAES-OCB %u:%u-bit", 1362 spacer, ik->ik_keyix+1, 8*keylen); 1363 break; 1364 case IEEE80211_CIPHER_AES_CCM: 1365 LINE_CHECK("%cAES-CCM %u:%u-bit", 1366 spacer, ik->ik_keyix+1, 8*keylen); 1367 break; 1368 case IEEE80211_CIPHER_CKIP: 1369 LINE_CHECK("%cCKIP %u:%u-bit", 1370 spacer, ik->ik_keyix+1, 8*keylen); 1371 break; 1372 case IEEE80211_CIPHER_NONE: 1373 LINE_CHECK("%cNULL %u:%u-bit", 1374 spacer, ik->ik_keyix+1, 8*keylen); 1375 break; 1376 default: 1377 LINE_CHECK("%cUNKNOWN (0x%x) %u:%u-bit", spacer, 1378 ik->ik_type, ik->ik_keyix+1, 8*keylen); 1379 break; 1380 } 1381 if (printcontents) { 1382 int i; 1383 1384 printf(" <"); 1385 for (i = 0; i < keylen; i++) 1386 printf("%02x", ik->ik_keydata[i]); 1387 printf(">"); 1388 if (ik->ik_type != IEEE80211_CIPHER_WEP && 1389 (ik->ik_keyrsc != 0 || verbose)) 1390 printf(" rsc %ju", (uintmax_t)ik->ik_keyrsc); 1391 if (ik->ik_type != IEEE80211_CIPHER_WEP && 1392 (ik->ik_keytsc != 0 || verbose)) 1393 printf(" tsc %ju", (uintmax_t)ik->ik_keytsc); 1394 if (ik->ik_flags != 0 && verbose) { 1395 const char *sep = " "; 1396 1397 if (ik->ik_flags & IEEE80211_KEY_XMIT) 1398 printf("%stx", sep), sep = "+"; 1399 if (ik->ik_flags & IEEE80211_KEY_RECV) 1400 printf("%srx", sep), sep = "+"; 1401 if (ik->ik_flags & IEEE80211_KEY_DEFAULT) 1402 printf("%sdef", sep), sep = "+"; 1403 } 1404 LINE_BREAK(); 1405 } 1406} 1407 1408static void 1409ieee80211_status(int s) 1410{ 1411 static const uint8_t zerobssid[IEEE80211_ADDR_LEN]; 1412 enum ieee80211_opmode opmode = get80211opmode(s); 1413 int i, num, wpa, wme; 1414 struct ieee80211req ireq; 1415 u_int8_t data[32]; 1416 const struct ieee80211_channel *c; 1417 1418 (void) memset(&ireq, 0, sizeof(ireq)); 1419 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 1420 ireq.i_data = &data; 1421 1422 wpa = 0; /* unknown/not set */ 1423 1424 ireq.i_type = IEEE80211_IOC_SSID; 1425 ireq.i_val = -1; 1426 if (ioctl(s, SIOCG80211, &ireq) < 0) { 1427 /* If we can't get the SSID, this isn't an 802.11 device. */ 1428 return; 1429 } 1430 num = 0; 1431 ireq.i_type = IEEE80211_IOC_NUMSSIDS; 1432 if (ioctl(s, SIOCG80211, &ireq) >= 0) 1433 num = ireq.i_val; 1434 printf("\tssid "); 1435 if (num > 1) { 1436 ireq.i_type = IEEE80211_IOC_SSID; 1437 for (ireq.i_val = 0; ireq.i_val < num; ireq.i_val++) { 1438 if (ioctl(s, SIOCG80211, &ireq) >= 0 && ireq.i_len > 0) { 1439 printf(" %d:", ireq.i_val + 1); 1440 print_string(data, ireq.i_len); 1441 } 1442 } 1443 } else 1444 print_string(data, ireq.i_len); 1445 1446 ireq.i_type = IEEE80211_IOC_CHANNEL; 1447 if (ioctl(s, SIOCG80211, &ireq) < 0) 1448 goto end; 1449 c = getchaninfo(s, ireq.i_val); 1450 if (ireq.i_val != -1) { 1451 printf(" channel %d", ireq.i_val); 1452 if (verbose) 1453 printf(" (%u)", c->ic_freq); 1454 } else if (verbose) 1455 printf(" channel UNDEF"); 1456 1457 ireq.i_type = IEEE80211_IOC_BSSID; 1458 ireq.i_len = IEEE80211_ADDR_LEN; 1459 if (ioctl(s, SIOCG80211, &ireq) >= 0 && 1460 (memcmp(ireq.i_data, zerobssid, sizeof(zerobssid)) != 0 || verbose)) 1461 printf(" bssid %s", ether_ntoa(ireq.i_data)); 1462 1463 ireq.i_type = IEEE80211_IOC_STATIONNAME; 1464 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1465 printf("\n\tstationname "); 1466 print_string(data, ireq.i_len); 1467 } 1468 1469 spacer = ' '; /* force first break */ 1470 LINE_BREAK(); 1471 1472 ireq.i_type = IEEE80211_IOC_AUTHMODE; 1473 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1474 switch (ireq.i_val) { 1475 case IEEE80211_AUTH_NONE: 1476 LINE_CHECK("%cauthmode NONE", spacer); 1477 break; 1478 case IEEE80211_AUTH_OPEN: 1479 LINE_CHECK("%cauthmode OPEN", spacer); 1480 break; 1481 case IEEE80211_AUTH_SHARED: 1482 LINE_CHECK("%cauthmode SHARED", spacer); 1483 break; 1484 case IEEE80211_AUTH_8021X: 1485 LINE_CHECK("%cauthmode 802.1x", spacer); 1486 break; 1487 case IEEE80211_AUTH_WPA: 1488 ireq.i_type = IEEE80211_IOC_WPA; 1489 if (ioctl(s, SIOCG80211, &ireq) != -1) 1490 wpa = ireq.i_val; 1491 if (!wpa) 1492 wpa = 1; /* default to WPA1 */ 1493 switch (wpa) { 1494 case 2: 1495 LINE_CHECK("%cauthmode WPA2/802.11i", 1496 spacer); 1497 break; 1498 case 3: 1499 LINE_CHECK("%cauthmode WPA1+WPA2/802.11i", 1500 spacer); 1501 break; 1502 default: 1503 LINE_CHECK("%cauthmode WPA", spacer); 1504 break; 1505 } 1506 break; 1507 case IEEE80211_AUTH_AUTO: 1508 LINE_CHECK("%cauthmode AUTO", spacer); 1509 break; 1510 default: 1511 LINE_CHECK("%cauthmode UNKNOWN (0x%x)", 1512 spacer, ireq.i_val); 1513 break; 1514 } 1515 } 1516 1517 ireq.i_type = IEEE80211_IOC_WEP; 1518 if (ioctl(s, SIOCG80211, &ireq) != -1 && 1519 ireq.i_val != IEEE80211_WEP_NOSUP) { 1520 int firstkey, wepmode; 1521 1522 wepmode = ireq.i_val; 1523 switch (wepmode) { 1524 case IEEE80211_WEP_OFF: 1525 LINE_CHECK("%cprivacy OFF", spacer); 1526 break; 1527 case IEEE80211_WEP_ON: 1528 LINE_CHECK("%cprivacy ON", spacer); 1529 break; 1530 case IEEE80211_WEP_MIXED: 1531 LINE_CHECK("%cprivacy MIXED", spacer); 1532 break; 1533 default: 1534 LINE_CHECK("%cprivacy UNKNOWN (0x%x)", 1535 spacer, wepmode); 1536 break; 1537 } 1538 1539 /* 1540 * If we get here then we've got WEP support so we need 1541 * to print WEP status. 1542 */ 1543 1544 ireq.i_type = IEEE80211_IOC_WEPTXKEY; 1545 if (ioctl(s, SIOCG80211, &ireq) < 0) { 1546 warn("WEP support, but no tx key!"); 1547 goto end; 1548 } 1549 if (ireq.i_val != -1) 1550 LINE_CHECK("%cdeftxkey %d", spacer, ireq.i_val+1); 1551 else if (wepmode != IEEE80211_WEP_OFF || verbose) 1552 LINE_CHECK("%cdeftxkey UNDEF", spacer); 1553 1554 ireq.i_type = IEEE80211_IOC_NUMWEPKEYS; 1555 if (ioctl(s, SIOCG80211, &ireq) < 0) { 1556 warn("WEP support, but no NUMWEPKEYS support!"); 1557 goto end; 1558 } 1559 num = ireq.i_val; 1560 1561 firstkey = 1; 1562 for (i = 0; i < num; i++) { 1563 struct ieee80211req_key ik; 1564 1565 memset(&ik, 0, sizeof(ik)); 1566 ik.ik_keyix = i; 1567 ireq.i_type = IEEE80211_IOC_WPAKEY; 1568 ireq.i_data = &ik; 1569 ireq.i_len = sizeof(ik); 1570 if (ioctl(s, SIOCG80211, &ireq) < 0) { 1571 warn("WEP support, but can get keys!"); 1572 goto end; 1573 } 1574 if (ik.ik_keylen != 0) { 1575 if (verbose) 1576 LINE_BREAK(); 1577 printkey(&ik); 1578 firstkey = 0; 1579 } 1580 } 1581 } 1582 1583 ireq.i_type = IEEE80211_IOC_POWERSAVE; 1584 if (ioctl(s, SIOCG80211, &ireq) != -1 && 1585 ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) { 1586 if (ireq.i_val != IEEE80211_POWERSAVE_OFF || verbose) { 1587 switch (ireq.i_val) { 1588 case IEEE80211_POWERSAVE_OFF: 1589 LINE_CHECK("%cpowersavemode OFF", 1590 spacer); 1591 break; 1592 case IEEE80211_POWERSAVE_CAM: 1593 LINE_CHECK("%cpowersavemode CAM", 1594 spacer); 1595 break; 1596 case IEEE80211_POWERSAVE_PSP: 1597 LINE_CHECK("%cpowersavemode PSP", 1598 spacer); 1599 break; 1600 case IEEE80211_POWERSAVE_PSP_CAM: 1601 LINE_CHECK("%cpowersavemode PSP-CAM", 1602 spacer); 1603 break; 1604 } 1605 ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP; 1606 if (ioctl(s, SIOCG80211, &ireq) != -1) 1607 LINE_CHECK("%cpowersavesleep %d", 1608 spacer, ireq.i_val); 1609 } 1610 } 1611 1612 ireq.i_type = IEEE80211_IOC_TXPOWMAX; 1613 if (ioctl(s, SIOCG80211, &ireq) != -1) 1614 LINE_CHECK("%ctxpowmax %d", spacer, ireq.i_val); 1615 1616 if (verbose) { 1617 ireq.i_type = IEEE80211_IOC_TXPOWER; 1618 if (ioctl(s, SIOCG80211, &ireq) != -1) 1619 LINE_CHECK("%ctxpower %d", spacer, ireq.i_val); 1620 } 1621 1622 ireq.i_type = IEEE80211_IOC_RTSTHRESHOLD; 1623 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1624 if (ireq.i_val != IEEE80211_RTS_MAX || verbose) 1625 LINE_CHECK("%crtsthreshold %d", spacer, ireq.i_val); 1626 } 1627 1628 ireq.i_type = IEEE80211_IOC_MCAST_RATE; 1629 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1630 if (ireq.i_val != 2*1 || verbose) { 1631 if (ireq.i_val == 11) 1632 LINE_CHECK("%cmcastrate 5.5", spacer); 1633 else 1634 LINE_CHECK("%cmcastrate %d", spacer, 1635 ireq.i_val/2); 1636 } 1637 } 1638 1639 ireq.i_type = IEEE80211_IOC_FRAGTHRESHOLD; 1640 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1641 if (ireq.i_val != IEEE80211_FRAG_MAX || verbose) 1642 LINE_CHECK("%cfragthreshold %d", spacer, ireq.i_val); 1643 } 1644 1645 if (IEEE80211_IS_CHAN_G(c) || IEEE80211_IS_CHAN_PUREG(c) || verbose) { 1646 ireq.i_type = IEEE80211_IOC_PUREG; 1647 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1648 if (ireq.i_val) 1649 LINE_CHECK("%cpureg", spacer); 1650 else if (verbose) 1651 LINE_CHECK("%c-pureg", spacer); 1652 } 1653 ireq.i_type = IEEE80211_IOC_PROTMODE; 1654 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1655 switch (ireq.i_val) { 1656 case IEEE80211_PROTMODE_OFF: 1657 LINE_CHECK("%cprotmode OFF", spacer); 1658 break; 1659 case IEEE80211_PROTMODE_CTS: 1660 LINE_CHECK("%cprotmode CTS", spacer); 1661 break; 1662 case IEEE80211_PROTMODE_RTSCTS: 1663 LINE_CHECK("%cprotmode RTSCTS", spacer); 1664 break; 1665 default: 1666 LINE_CHECK("%cprotmode UNKNOWN (0x%x)", 1667 spacer, ireq.i_val); 1668 break; 1669 } 1670 } 1671 } 1672 1673 ireq.i_type = IEEE80211_IOC_WME; 1674 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1675 wme = ireq.i_val; 1676 if (wme) 1677 LINE_CHECK("%cwme", spacer); 1678 else if (verbose) 1679 LINE_CHECK("%c-wme", spacer); 1680 } else 1681 wme = 0; 1682 1683 ireq.i_type = IEEE80211_IOC_BURST; 1684 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1685 if (ireq.i_val) 1686 LINE_CHECK("%cburst", spacer); 1687 else if (verbose) 1688 LINE_CHECK("%c-burst", spacer); 1689 } 1690 1691 if (opmode == IEEE80211_M_HOSTAP) { 1692 ireq.i_type = IEEE80211_IOC_HIDESSID; 1693 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1694 if (ireq.i_val) 1695 LINE_CHECK("%cssid HIDE", spacer); 1696 else if (verbose) 1697 LINE_CHECK("%cssid SHOW", spacer); 1698 } 1699 1700 ireq.i_type = IEEE80211_IOC_APBRIDGE; 1701 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1702 if (!ireq.i_val) 1703 LINE_CHECK("%c-apbridge", spacer); 1704 else if (verbose) 1705 LINE_CHECK("%capbridge", spacer); 1706 } 1707 1708 ireq.i_type = IEEE80211_IOC_DTIM_PERIOD; 1709 if (ioctl(s, SIOCG80211, &ireq) != -1) 1710 LINE_CHECK("%cdtimperiod %u", spacer, ireq.i_val); 1711 } else { 1712 ireq.i_type = IEEE80211_IOC_ROAMING; 1713 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1714 if (ireq.i_val != IEEE80211_ROAMING_AUTO || verbose) { 1715 switch (ireq.i_val) { 1716 case IEEE80211_ROAMING_DEVICE: 1717 LINE_CHECK("%croaming DEVICE", spacer); 1718 break; 1719 case IEEE80211_ROAMING_AUTO: 1720 LINE_CHECK("%croaming AUTO", spacer); 1721 break; 1722 case IEEE80211_ROAMING_MANUAL: 1723 LINE_CHECK("%croaming MANUAL", spacer); 1724 break; 1725 default: 1726 LINE_CHECK("%croaming UNKNOWN (0x%x)", 1727 spacer, ireq.i_val); 1728 break; 1729 } 1730 } 1731 } 1732 } 1733 ireq.i_type = IEEE80211_IOC_BEACON_INTERVAL; 1734 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1735 if (ireq.i_val) 1736 LINE_CHECK("%cbintval %u", spacer, ireq.i_val); 1737 else if (verbose) 1738 LINE_CHECK("%cbintval %u", spacer, ireq.i_val); 1739 } 1740 1741 if (wme && verbose) { 1742 LINE_BREAK(); 1743 list_wme(s); 1744 } 1745 1746 if (wpa) { 1747 ireq.i_type = IEEE80211_IOC_COUNTERMEASURES; 1748 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1749 if (ireq.i_val) 1750 LINE_CHECK("%ccountermeasures", spacer); 1751 else if (verbose) 1752 LINE_CHECK("%c-countermeasures", spacer); 1753 } 1754#if 0 1755 /* XXX not interesting with WPA done in user space */ 1756 ireq.i_type = IEEE80211_IOC_KEYMGTALGS; 1757 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1758 } 1759 1760 ireq.i_type = IEEE80211_IOC_MCASTCIPHER; 1761 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1762 printf("%cmcastcipher ", spacer); 1763 printcipher(s, &ireq, IEEE80211_IOC_MCASTKEYLEN); 1764 spacer = ' '; 1765 } 1766 1767 ireq.i_type = IEEE80211_IOC_UCASTCIPHER; 1768 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1769 printf("%cucastcipher ", spacer); 1770 printcipher(s, &ireq, IEEE80211_IOC_UCASTKEYLEN); 1771 } 1772 1773 if (wpa & 2) { 1774 ireq.i_type = IEEE80211_IOC_RSNCAPS; 1775 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1776 printf("%cRSN caps 0x%x", spacer, ireq.i_val); 1777 spacer = ' '; 1778 } 1779 } 1780 1781 ireq.i_type = IEEE80211_IOC_UCASTCIPHERS; 1782 if (ioctl(s, SIOCG80211, &ireq) != -1) { 1783 } 1784#endif 1785 LINE_BREAK(); 1786 } 1787 LINE_BREAK(); 1788 1789end: 1790 return; 1791} 1792 1793static void 1794set80211(int s, int type, int val, int len, u_int8_t *data) 1795{ 1796 struct ieee80211req ireq; 1797 1798 (void) memset(&ireq, 0, sizeof(ireq)); 1799 (void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); 1800 ireq.i_type = type; 1801 ireq.i_val = val; 1802 ireq.i_len = len; 1803 ireq.i_data = data; 1804 if (ioctl(s, SIOCS80211, &ireq) < 0) 1805 err(1, "SIOCS80211"); 1806} 1807 1808static const char * 1809get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp) 1810{ 1811 int len; 1812 int hexstr; 1813 u_int8_t *p; 1814 1815 len = *lenp; 1816 p = buf; 1817 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x'); 1818 if (hexstr) 1819 val += 2; 1820 for (;;) { 1821 if (*val == '\0') 1822 break; 1823 if (sep != NULL && strchr(sep, *val) != NULL) { 1824 val++; 1825 break; 1826 } 1827 if (hexstr) { 1828 if (!isxdigit((u_char)val[0])) { 1829 warnx("bad hexadecimal digits"); 1830 return NULL; 1831 } 1832 if (!isxdigit((u_char)val[1])) { 1833 warnx("odd count hexadecimal digits"); 1834 return NULL; 1835 } 1836 } 1837 if (p >= buf + len) { 1838 if (hexstr) 1839 warnx("hexadecimal digits too long"); 1840 else 1841 warnx("string too long"); 1842 return NULL; 1843 } 1844 if (hexstr) { 1845#define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10) 1846 *p++ = (tohex((u_char)val[0]) << 4) | 1847 tohex((u_char)val[1]); 1848#undef tohex 1849 val += 2; 1850 } else 1851 *p++ = *val++; 1852 } 1853 len = p - buf; 1854 /* The string "-" is treated as the empty string. */ 1855 if (!hexstr && len == 1 && buf[0] == '-') 1856 len = 0; 1857 if (len < *lenp) 1858 memset(p, 0, *lenp - len); 1859 *lenp = len; 1860 return val; 1861} 1862 1863static void 1864print_string(const u_int8_t *buf, int len) 1865{ 1866 int i; 1867 int hasspc; 1868 1869 i = 0; 1870 hasspc = 0; 1871 for (; i < len; i++) { 1872 if (!isprint(buf[i]) && buf[i] != '\0') 1873 break; 1874 if (isspace(buf[i])) 1875 hasspc++; 1876 } 1877 if (i == len) { 1878 if (hasspc || len == 0 || buf[0] == '\0') 1879 printf("\"%.*s\"", len, buf); 1880 else 1881 printf("%.*s", len, buf); 1882 } else { 1883 printf("0x"); 1884 for (i = 0; i < len; i++) 1885 printf("%02x", buf[i]); 1886 } 1887} 1888 1889static struct cmd ieee80211_cmds[] = { 1890 DEF_CMD_ARG("ssid", set80211ssid), 1891 DEF_CMD_ARG("nwid", set80211ssid), 1892 DEF_CMD_ARG("stationname", set80211stationname), 1893 DEF_CMD_ARG("station", set80211stationname), /* BSD/OS */ 1894 DEF_CMD_ARG("channel", set80211channel), 1895 DEF_CMD_ARG("authmode", set80211authmode), 1896 DEF_CMD_ARG("powersavemode", set80211powersavemode), 1897 DEF_CMD("powersave", 1, set80211powersave), 1898 DEF_CMD("-powersave", 0, set80211powersave), 1899 DEF_CMD_ARG("powersavesleep", set80211powersavesleep), 1900 DEF_CMD_ARG("wepmode", set80211wepmode), 1901 DEF_CMD("wep", 1, set80211wep), 1902 DEF_CMD("-wep", 0, set80211wep), 1903 DEF_CMD_ARG("deftxkey", set80211weptxkey), 1904 DEF_CMD_ARG("weptxkey", set80211weptxkey), 1905 DEF_CMD_ARG("wepkey", set80211wepkey), 1906 DEF_CMD_ARG("nwkey", set80211nwkey), /* NetBSD */ 1907 DEF_CMD("-nwkey", 0, set80211wep), /* NetBSD */ 1908 DEF_CMD_ARG("rtsthreshold", set80211rtsthreshold), 1909 DEF_CMD_ARG("protmode", set80211protmode), 1910 DEF_CMD_ARG("txpower", set80211txpower), 1911 DEF_CMD_ARG("roaming", set80211roaming), 1912 DEF_CMD("wme", 1, set80211wme), 1913 DEF_CMD("-wme", 0, set80211wme), 1914 DEF_CMD("hidessid", 1, set80211hidessid), 1915 DEF_CMD("-hidessid", 0, set80211hidessid), 1916 DEF_CMD("apbridge", 1, set80211apbridge), 1917 DEF_CMD("-apbridge", 0, set80211apbridge), 1918 DEF_CMD_ARG("chanlist", set80211chanlist), 1919 DEF_CMD_ARG("bssid", set80211bssid), 1920 DEF_CMD_ARG("ap", set80211bssid), 1921 DEF_CMD("scan", 0, set80211scan), 1922 DEF_CMD_ARG("list", set80211list), 1923 DEF_CMD_ARG2("cwmin", set80211cwmin), 1924 DEF_CMD_ARG2("cwmax", set80211cwmax), 1925 DEF_CMD_ARG2("aifs", set80211aifs), 1926 DEF_CMD_ARG2("txoplimit", set80211txoplimit), 1927 DEF_CMD_ARG("acm", set80211acm), 1928 DEF_CMD_ARG("-acm", set80211noacm), 1929 DEF_CMD_ARG("ack", set80211ackpolicy), 1930 DEF_CMD_ARG("-ack", set80211noackpolicy), 1931 DEF_CMD_ARG2("bss:cwmin", set80211bsscwmin), 1932 DEF_CMD_ARG2("bss:cwmax", set80211bsscwmax), 1933 DEF_CMD_ARG2("bss:aifs", set80211bssaifs), 1934 DEF_CMD_ARG2("bss:txoplimit", set80211bsstxoplimit), 1935 DEF_CMD_ARG("dtimperiod", set80211dtimperiod), 1936 DEF_CMD_ARG("bintval", set80211bintval), 1937 DEF_CMD("mac:open", IEEE80211_MACCMD_POLICY_OPEN, set80211maccmd), 1938 DEF_CMD("mac:allow", IEEE80211_MACCMD_POLICY_ALLOW, set80211maccmd), 1939 DEF_CMD("mac:deny", IEEE80211_MACCMD_POLICY_DENY, set80211maccmd), 1940 DEF_CMD("mac:flush", IEEE80211_MACCMD_FLUSH, set80211maccmd), 1941 DEF_CMD("mac:detach", IEEE80211_MACCMD_DETACH, set80211maccmd), 1942 DEF_CMD_ARG("mac:add", set80211addmac), 1943 DEF_CMD_ARG("mac:del", set80211delmac), 1944 DEF_CMD_ARG("mac:kick", set80211kickmac), 1945 DEF_CMD("pureg", 1, set80211pureg), 1946 DEF_CMD("-pureg", 0, set80211pureg), 1947 DEF_CMD_ARG("mcastrate", set80211mcastrate), 1948 DEF_CMD_ARG("fragthreshold", set80211fragthreshold), 1949 DEF_CMD("burst", 1, set80211burst), 1950 DEF_CMD("-burst", 0, set80211burst), 1951}; 1952static struct afswtch af_ieee80211 = { 1953 .af_name = "af_ieee80211", 1954 .af_af = AF_UNSPEC, 1955 .af_other_status = ieee80211_status, 1956}; 1957 1958static __constructor void 1959ieee80211_ctor(void) 1960{ 1961#define N(a) (sizeof(a) / sizeof(a[0])) 1962 int i; 1963 1964 for (i = 0; i < N(ieee80211_cmds); i++) 1965 cmd_register(&ieee80211_cmds[i]); 1966 af_register(&af_ieee80211); 1967#undef N 1968} 1969