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