1 /* 2 * iwinfo - Wireless Information Library - NL80211 Backend 3 * 4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org> 5 * 6 * The iwinfo library is free software: you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License version 2 8 * as published by the Free Software Foundation. 9 * 10 * The iwinfo library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 * See the GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/. 17 * 18 * The signal handling code is derived from the official madwifi tools, 19 * wlanconfig.c in particular. The encryption property handling was 20 * inspired by the hostapd madwifi driver. 21 * 22 * Parts of this code are derived from the Linux iw utility. 23 */ 24 25 #include <sys/stat.h> 26 #include <limits.h> 27 #include <glob.h> 28 #include <fnmatch.h> 29 #include <stdarg.h> 30 #include <stdlib.h> 31 32 #include "iwinfo_nl80211.h" 33 34 #define min(x, y) ((x) < (y)) ? (x) : (y) 35 36 #define BIT(x) (1ULL<<(x)) 37 38 static struct nl80211_state *nls = NULL; 39 40 static void nl80211_close(void) 41 { 42 if (nls) 43 { 44 if (nls->nlctrl) 45 genl_family_put(nls->nlctrl); 46 47 if (nls->nl80211) 48 genl_family_put(nls->nl80211); 49 50 if (nls->nl_sock) 51 nl_socket_free(nls->nl_sock); 52 53 if (nls->nl_cache) 54 nl_cache_free(nls->nl_cache); 55 56 free(nls); 57 nls = NULL; 58 } 59 } 60 61 static int nl80211_init(void) 62 { 63 int err, fd; 64 65 if (!nls) 66 { 67 nls = malloc(sizeof(struct nl80211_state)); 68 if (!nls) { 69 err = -ENOMEM; 70 goto err; 71 } 72 73 memset(nls, 0, sizeof(*nls)); 74 75 nls->nl_sock = nl_socket_alloc(); 76 if (!nls->nl_sock) { 77 err = -ENOMEM; 78 goto err; 79 } 80 81 if (genl_connect(nls->nl_sock)) { 82 err = -ENOLINK; 83 goto err; 84 } 85 86 fd = nl_socket_get_fd(nls->nl_sock); 87 if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) { 88 err = -EINVAL; 89 goto err; 90 } 91 92 if (genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) { 93 err = -ENOMEM; 94 goto err; 95 } 96 97 nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211"); 98 if (!nls->nl80211) { 99 err = -ENOENT; 100 goto err; 101 } 102 103 nls->nlctrl = genl_ctrl_search_by_name(nls->nl_cache, "nlctrl"); 104 if (!nls->nlctrl) { 105 err = -ENOENT; 106 goto err; 107 } 108 } 109 110 return 0; 111 112 113 err: 114 nl80211_close(); 115 return err; 116 } 117 118 static int nl80211_readint(const char *path) 119 { 120 int fd; 121 int rv = -1; 122 char buffer[16]; 123 124 if ((fd = open(path, O_RDONLY)) > -1) 125 { 126 if (read(fd, buffer, sizeof(buffer)) > 0) 127 rv = atoi(buffer); 128 129 close(fd); 130 } 131 132 return rv; 133 } 134 135 static int nl80211_readstr(const char *path, char *buffer, int length) 136 { 137 int fd; 138 int rv = -1; 139 140 if ((fd = open(path, O_RDONLY)) > -1) 141 { 142 if ((rv = read(fd, buffer, length - 1)) > 0) 143 { 144 if (buffer[rv - 1] == '\n') 145 rv--; 146 147 buffer[rv] = 0; 148 } 149 150 close(fd); 151 } 152 153 return rv; 154 } 155 156 static int nl80211_get_band(int nla_type) 157 { 158 switch (nla_type) 159 { 160 case NL80211_BAND_2GHZ: 161 return IWINFO_BAND_24; 162 case NL80211_BAND_5GHZ: 163 return IWINFO_BAND_5; 164 case NL80211_BAND_6GHZ: 165 return IWINFO_BAND_6; 166 case NL80211_BAND_60GHZ: 167 return IWINFO_BAND_60; 168 } 169 170 return 0; 171 } 172 173 174 static int nl80211_msg_error(struct sockaddr_nl *nla, 175 struct nlmsgerr *err, void *arg) 176 { 177 int *ret = arg; 178 *ret = err->error; 179 return NL_STOP; 180 } 181 182 static int nl80211_msg_finish(struct nl_msg *msg, void *arg) 183 { 184 int *ret = arg; 185 *ret = 0; 186 return NL_SKIP; 187 } 188 189 static int nl80211_msg_ack(struct nl_msg *msg, void *arg) 190 { 191 int *ret = arg; 192 *ret = 0; 193 return NL_STOP; 194 } 195 196 static int nl80211_msg_response(struct nl_msg *msg, void *arg) 197 { 198 return NL_SKIP; 199 } 200 201 static void nl80211_free(struct nl80211_msg_conveyor *cv) 202 { 203 if (cv) 204 { 205 if (cv->cb) 206 nl_cb_put(cv->cb); 207 208 if (cv->msg) 209 nlmsg_free(cv->msg); 210 211 cv->cb = NULL; 212 cv->msg = NULL; 213 } 214 } 215 216 static struct nl80211_msg_conveyor * nl80211_new(struct genl_family *family, 217 int cmd, int flags) 218 { 219 static struct nl80211_msg_conveyor cv; 220 221 struct nl_msg *req = NULL; 222 struct nl_cb *cb = NULL; 223 224 req = nlmsg_alloc(); 225 if (!req) 226 goto err; 227 228 cb = nl_cb_alloc(NL_CB_DEFAULT); 229 if (!cb) 230 goto err; 231 232 genlmsg_put(req, 0, 0, genl_family_get_id(family), 0, flags, cmd, 0); 233 234 cv.msg = req; 235 cv.cb = cb; 236 237 return &cv; 238 239 err: 240 if (req) 241 nlmsg_free(req); 242 243 return NULL; 244 } 245 246 static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags) 247 { 248 if (nl80211_init() < 0) 249 return NULL; 250 251 return nl80211_new(nls->nlctrl, cmd, flags); 252 } 253 254 static const char *nl80211_phy_path_str(const char *phyname) 255 { 256 static char path[PATH_MAX]; 257 const char *prefix = "/sys/devices/"; 258 int prefix_len = strlen(prefix); 259 int buf_len, offset; 260 struct dirent *e; 261 char buf[512], *link; 262 int phy_idx; 263 int seq = 0; 264 DIR *d; 265 266 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", phyname); 267 phy_idx = nl80211_readint(buf); 268 if (phy_idx < 0) 269 return NULL; 270 271 buf_len = snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", phyname); 272 link = realpath(buf, path); 273 if (!link) 274 return NULL; 275 276 if (strncmp(link, prefix, prefix_len) != 0) 277 return NULL; 278 279 link += prefix_len; 280 281 prefix = "platform/"; 282 prefix_len = strlen(prefix); 283 if (!strncmp(link, prefix, prefix_len) && strstr(link, "/pci")) 284 link += prefix_len; 285 286 snprintf(buf + buf_len, sizeof(buf) - buf_len, "/ieee80211"); 287 d = opendir(buf); 288 if (!d) 289 return link; 290 291 while ((e = readdir(d)) != NULL) { 292 int cur_idx; 293 294 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name); 295 cur_idx = nl80211_readint(buf); 296 if (cur_idx < 0) 297 continue; 298 299 if (cur_idx >= phy_idx) 300 continue; 301 302 seq++; 303 } 304 305 closedir(d); 306 307 if (!seq) 308 return link; 309 310 offset = link - path + strlen(link); 311 snprintf(path + offset, sizeof(path) - offset, "+%d", seq); 312 313 return link; 314 } 315 316 static int nl80211_phy_idx_from_path(const char *path) 317 { 318 char buf[512]; 319 struct dirent *e; 320 const char *cur_path; 321 int cur_path_len; 322 int path_len; 323 int idx = -1; 324 DIR *d; 325 326 if (!path) 327 return -1; 328 329 path_len = strlen(path); 330 if (!path_len) 331 return -1; 332 333 d = opendir("/sys/class/ieee80211"); 334 if (!d) 335 return -1; 336 337 while ((e = readdir(d)) != NULL) { 338 cur_path = nl80211_phy_path_str(e->d_name); 339 if (!cur_path) 340 continue; 341 342 cur_path_len = strlen(cur_path); 343 if (cur_path_len < path_len) 344 continue; 345 346 if (strcmp(cur_path + cur_path_len - path_len, path) != 0) 347 continue; 348 349 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name); 350 idx = nl80211_readint(buf); 351 352 if (idx >= 0) 353 break; 354 } 355 356 closedir(d); 357 358 return idx; 359 } 360 361 static int nl80211_phy_idx_from_macaddr(const char *opt) 362 { 363 char buf[128]; 364 int i, idx = -1; 365 glob_t gl; 366 367 if (!opt) 368 return -1; 369 370 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*"); 371 if (glob(buf, 0, NULL, &gl)) 372 return -1; 373 374 for (i = 0; i < gl.gl_pathc; i++) 375 { 376 snprintf(buf, sizeof(buf), "%s/macaddress", gl.gl_pathv[i]); 377 if (nl80211_readstr(buf, buf, sizeof(buf)) <= 0) 378 continue; 379 380 if (fnmatch(opt, buf, FNM_CASEFOLD)) 381 continue; 382 383 snprintf(buf, sizeof(buf), "%s/index", gl.gl_pathv[i]); 384 if ((idx = nl80211_readint(buf)) > -1) 385 break; 386 } 387 388 globfree(&gl); 389 390 return idx; 391 } 392 393 static int nl80211_phy_idx_from_phy(const char *opt) 394 { 395 char buf[128]; 396 397 if (!opt) 398 return -1; 399 400 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", opt); 401 return nl80211_readint(buf); 402 } 403 404 static int nl80211_phy_idx_from_uci(const char *name) 405 { 406 struct uci_section *s; 407 const char *opt; 408 int idx = -1; 409 410 s = iwinfo_uci_get_radio(name, "mac80211"); 411 if (!s) 412 goto out; 413 414 opt = uci_lookup_option_string(uci_ctx, s, "path"); 415 idx = nl80211_phy_idx_from_path(opt); 416 if (idx >= 0) 417 goto out; 418 419 opt = uci_lookup_option_string(uci_ctx, s, "macaddr"); 420 idx = nl80211_phy_idx_from_macaddr(opt); 421 if (idx >= 0) 422 goto out; 423 424 opt = uci_lookup_option_string(uci_ctx, s, "phy"); 425 idx = nl80211_phy_idx_from_phy(opt); 426 427 out: 428 iwinfo_uci_free(); 429 return idx; 430 } 431 432 static bool nl80211_is_ifname(const char *name) 433 { 434 struct stat st; 435 char buffer[PATH_MAX]; 436 437 snprintf(buffer, sizeof(buffer), "/sys/class/net/%s", name); 438 return !lstat(buffer, &st); 439 } 440 441 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, 442 int cmd, int flags) 443 { 444 unsigned int ifidx = 0; 445 int phyidx = -1; 446 struct nl80211_msg_conveyor *cv; 447 448 if (ifname == NULL) 449 return NULL; 450 451 if (nl80211_init() < 0) 452 return NULL; 453 454 if (!strncmp(ifname, "mon.", 4)) 455 ifidx = if_nametoindex(&ifname[4]); 456 else if (nl80211_is_ifname(ifname)) 457 ifidx = if_nametoindex(ifname); 458 else 459 { 460 phyidx = nl80211_phy_idx_from_phy(ifname); 461 if (phyidx < 0) 462 phyidx = nl80211_phy_idx_from_uci(ifname); 463 } 464 465 /* Valid ifidx must be greater than 0 */ 466 if ((ifidx <= 0) && (phyidx < 0)) 467 return NULL; 468 469 cv = nl80211_new(nls->nl80211, cmd, flags); 470 if (!cv) 471 return NULL; 472 473 if (ifidx > 0) 474 NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx); 475 else if (phyidx > -1) 476 NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx); 477 478 return cv; 479 480 nla_put_failure: 481 nl80211_free(cv); 482 return NULL; 483 } 484 485 static int nl80211_send(struct nl80211_msg_conveyor *cv, 486 int (*cb_func)(struct nl_msg *, void *), 487 void *cb_arg) 488 { 489 static struct nl80211_msg_conveyor rcv; 490 int err; 491 492 if (cb_func) 493 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg); 494 else 495 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv); 496 497 err = nl_send_auto_complete(nls->nl_sock, cv->msg); 498 499 if (err < 0) 500 goto out; 501 502 err = 1; 503 504 nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err); 505 nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err); 506 nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err); 507 508 while (err > 0) 509 nl_recvmsgs(nls->nl_sock, cv->cb); 510 511 out: 512 nl80211_free(cv); 513 return err; 514 } 515 516 static int nl80211_request(const char *ifname, int cmd, int flags, 517 int (*cb_func)(struct nl_msg *, void *), 518 void *cb_arg) 519 { 520 struct nl80211_msg_conveyor *cv; 521 522 cv = nl80211_msg(ifname, cmd, flags); 523 524 if (!cv) 525 return -ENOMEM; 526 527 return nl80211_send(cv, cb_func, cb_arg); 528 } 529 530 static struct nlattr ** nl80211_parse(struct nl_msg *msg) 531 { 532 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 533 static struct nlattr *attr[NL80211_ATTR_MAX + 1]; 534 535 nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), 536 genlmsg_attrlen(gnlh, 0), NULL); 537 538 return attr; 539 } 540 541 static int nl80211_get_protocol_features_cb(struct nl_msg *msg, void *arg) 542 { 543 uint32_t *features = arg; 544 struct nlattr **attr = nl80211_parse(msg); 545 546 if (attr[NL80211_ATTR_PROTOCOL_FEATURES]) 547 *features = nla_get_u32(attr[NL80211_ATTR_PROTOCOL_FEATURES]); 548 549 return NL_SKIP; 550 } 551 552 static int nl80211_get_protocol_features(const char *ifname) 553 { 554 struct nl80211_msg_conveyor *req; 555 uint32_t features = 0; 556 557 req = nl80211_msg(ifname, NL80211_CMD_GET_PROTOCOL_FEATURES, 0); 558 if (req) { 559 nl80211_send(req, nl80211_get_protocol_features_cb, &features); 560 nl80211_free(req); 561 } 562 563 return features; 564 } 565 566 static int nl80211_subscribe_cb(struct nl_msg *msg, void *arg) 567 { 568 struct nl80211_group_conveyor *cv = arg; 569 570 struct nlattr **attr = nl80211_parse(msg); 571 struct nlattr *mgrpinfo[CTRL_ATTR_MCAST_GRP_MAX + 1]; 572 struct nlattr *mgrp; 573 int mgrpidx; 574 575 if (!attr[CTRL_ATTR_MCAST_GROUPS]) 576 return NL_SKIP; 577 578 nla_for_each_nested(mgrp, attr[CTRL_ATTR_MCAST_GROUPS], mgrpidx) 579 { 580 nla_parse(mgrpinfo, CTRL_ATTR_MCAST_GRP_MAX, 581 nla_data(mgrp), nla_len(mgrp), NULL); 582 583 if (mgrpinfo[CTRL_ATTR_MCAST_GRP_ID] && 584 mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME] && 585 !strncmp(nla_data(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]), 586 cv->name, nla_len(mgrpinfo[CTRL_ATTR_MCAST_GRP_NAME]))) 587 { 588 cv->id = nla_get_u32(mgrpinfo[CTRL_ATTR_MCAST_GRP_ID]); 589 break; 590 } 591 } 592 593 return NL_SKIP; 594 } 595 596 static int nl80211_subscribe(const char *family, const char *group) 597 { 598 struct nl80211_group_conveyor cv = { .name = group, .id = -ENOENT }; 599 struct nl80211_msg_conveyor *req; 600 int err; 601 602 req = nl80211_ctl(CTRL_CMD_GETFAMILY, 0); 603 if (req) 604 { 605 NLA_PUT_STRING(req->msg, CTRL_ATTR_FAMILY_NAME, family); 606 err = nl80211_send(req, nl80211_subscribe_cb, &cv); 607 608 if (err) 609 return err; 610 611 return nl_socket_add_membership(nls->nl_sock, cv.id); 612 613 nla_put_failure: 614 nl80211_free(req); 615 } 616 617 return -ENOMEM; 618 } 619 620 621 static int nl80211_wait_cb(struct nl_msg *msg, void *arg) 622 { 623 struct nl80211_event_conveyor *cv = arg; 624 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 625 626 if (cv->wait[gnlh->cmd / 32] & (1 << (gnlh->cmd % 32))) 627 cv->recv = gnlh->cmd; 628 629 return NL_SKIP; 630 } 631 632 static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg) 633 { 634 return NL_OK; 635 } 636 637 static int __nl80211_wait(const char *family, const char *group, ...) 638 { 639 struct nl80211_event_conveyor cv = { }; 640 struct nl_cb *cb; 641 int err = 0; 642 int cmd; 643 va_list ap; 644 645 if (nl80211_subscribe(family, group)) 646 return -ENOENT; 647 648 cb = nl_cb_alloc(NL_CB_DEFAULT); 649 650 if (!cb) 651 return -ENOMEM; 652 653 nl_cb_err(cb, NL_CB_CUSTOM, nl80211_msg_error, &err); 654 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL); 655 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_wait_cb, &cv ); 656 657 va_start(ap, group); 658 659 for (cmd = va_arg(ap, int); cmd != 0; cmd = va_arg(ap, int)) 660 cv.wait[cmd / 32] |= (1 << (cmd % 32)); 661 662 va_end(ap); 663 664 while (!cv.recv && !err) 665 nl_recvmsgs(nls->nl_sock, cb); 666 667 nl_cb_put(cb); 668 669 return err; 670 } 671 672 #define nl80211_wait(family, group, ...) \ 673 __nl80211_wait(family, group, __VA_ARGS__, 0) 674 675 /* This is linux's ieee80211_freq_khz_to_channel() which is: 676 * SPDX-License-Identifier: GPL-2.0 677 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net> 678 * Copyright 2013-2014 Intel Mobile Communications GmbH 679 * Copyright 2017 Intel Deutschland GmbH 680 * Copyright (C) 2018-2022 Intel Corporation 681 */ 682 static int nl80211_freq2channel(int freq) 683 { 684 if (freq == 2484) 685 return 14; 686 else if (freq < 2484) 687 return (freq - 2407) / 5; 688 else if (freq >= 4910 && freq <= 4980) 689 return (freq - 4000) / 5; 690 else if (freq < 5925) 691 return (freq - 5000) / 5; 692 else if (freq == 5935) 693 return 2; 694 else if (freq <= 45000) /* DMG band lower limit */ 695 /* see 802.11ax D6.1 27.3.22.2 */ 696 return (freq - 5950) / 5; 697 else if (freq >= 58320 && freq <= 70200) 698 return (freq - 56160) / 2160; 699 else 700 return 0; 701 } 702 703 /* This is linux's ieee80211_channel_to_freq_khz() which is: 704 * SPDX-License-Identifier: GPL-2.0 705 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net> 706 * Copyright 2013-2014 Intel Mobile Communications GmbH 707 * Copyright 2017 Intel Deutschland GmbH 708 * Copyright (C) 2018-2022 Intel Corporation 709 */ 710 static int nl80211_channel2freq(int channel, const char *band, bool ax) 711 { 712 if (channel < 1) 713 return 0; 714 715 if (!band || band[0] != 'a') 716 { 717 if (channel == 14) 718 return 2484; 719 else if (channel < 14) 720 return (channel * 5) + 2407; 721 } 722 else if (strcmp(band, "ad") == 0) 723 { 724 if (channel < 7) 725 return 56160 + 2160 * channel; 726 } 727 else if (ax) 728 { 729 if (channel == 2) 730 return 5935; 731 if (channel < 233) 732 return (channel * 5) + 5950; 733 } 734 else 735 { 736 if (channel >= 182 && channel <= 196) 737 return (channel * 5) + 4000; 738 else 739 return (channel * 5) + 5000; 740 } 741 742 return 0; 743 } 744 745 static uint8_t nl80211_freq2band(int freq) 746 { 747 if (freq >= 2412 && freq <= 2484) 748 return IWINFO_BAND_24; 749 else if (freq >= 5160 && freq <= 5885) 750 return IWINFO_BAND_5; 751 else if (freq >= 5925 && freq <= 7125) 752 return IWINFO_BAND_6; 753 else if (freq >= 58320 && freq <= 69120) 754 return IWINFO_BAND_60; 755 756 return 0; 757 } 758 759 static int nl80211_phyname_cb(struct nl_msg *msg, void *arg) 760 { 761 char *buf = arg; 762 struct nlattr **attr = nl80211_parse(msg); 763 764 if (attr[NL80211_ATTR_WIPHY_NAME]) 765 memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]), 766 nla_len(attr[NL80211_ATTR_WIPHY_NAME])); 767 else 768 buf[0] = 0; 769 770 return NL_SKIP; 771 } 772 773 static char * nl80211_ifname2phy(const char *ifname) 774 { 775 static char phy[32] = { 0 }; 776 777 memset(phy, 0, sizeof(phy)); 778 779 nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, 780 nl80211_phyname_cb, phy); 781 782 return phy[0] ? phy : NULL; 783 } 784 785 static char * nl80211_phyidx2name(unsigned int idx) 786 { 787 struct nl80211_msg_conveyor *cv; 788 static char phy[32] = { 0 }; 789 790 if (nl80211_init() < 0) 791 return NULL; 792 793 cv = nl80211_new(nls->nl80211, NL80211_CMD_GET_WIPHY, 0); 794 if (!cv) 795 return NULL; 796 797 NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, idx); 798 799 memset(phy, 0, sizeof(phy)); 800 nl80211_send(cv, nl80211_phyname_cb, phy); 801 802 return phy[0] ? phy : NULL; 803 804 nla_put_failure: 805 return NULL; 806 } 807 808 static char * nl80211_phy2ifname(const char *ifname) 809 { 810 int ifidx = -1, cifidx, lmode = 1, clmode, phyidx; 811 char buffer[512]; 812 static char nif[IFNAMSIZ] = { 0 }; 813 DIR *d; 814 struct dirent *e; 815 816 /* Only accept phy name in the form of phy%d or radio%d */ 817 if (!ifname) 818 return NULL; 819 820 phyidx = nl80211_phy_idx_from_phy(ifname); 821 if (phyidx < 0) 822 phyidx = nl80211_phy_idx_from_uci(ifname);; 823 if (phyidx < 0) 824 return NULL; 825 826 memset(nif, 0, sizeof(nif)); 827 828 if ((d = opendir("/sys/class/net")) != NULL) 829 { 830 while ((e = readdir(d)) != NULL) 831 { 832 snprintf(buffer, sizeof(buffer), 833 "/sys/class/net/%s/phy80211/index", e->d_name); 834 if (nl80211_readint(buffer) != phyidx) 835 continue; 836 837 snprintf(buffer, sizeof(buffer), 838 "/sys/class/net/%s/ifindex", e->d_name); 839 cifidx = nl80211_readint(buffer); 840 841 if (cifidx < 0) 842 continue; 843 844 snprintf(buffer, sizeof(buffer), 845 "/sys/class/net/%s/link_mode", e->d_name); 846 clmode = nl80211_readint(buffer); 847 848 /* prefer non-supplicant-based devices */ 849 if ((ifidx < 0) || (cifidx < ifidx) || ((lmode == 1) && (clmode != 1))) 850 { 851 ifidx = cifidx; 852 lmode = clmode; 853 strncpy(nif, e->d_name, sizeof(nif) - 1); 854 } 855 } 856 857 closedir(d); 858 } 859 860 return nif[0] ? nif : NULL; 861 } 862 863 static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg) 864 { 865 int *mode = arg; 866 struct nlattr **tb = nl80211_parse(msg); 867 const int ifmodes[NL80211_IFTYPE_MAX + 1] = { 868 IWINFO_OPMODE_UNKNOWN, /* unspecified */ 869 IWINFO_OPMODE_ADHOC, /* IBSS */ 870 IWINFO_OPMODE_CLIENT, /* managed */ 871 IWINFO_OPMODE_MASTER, /* AP */ 872 IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */ 873 IWINFO_OPMODE_WDS, /* WDS */ 874 IWINFO_OPMODE_MONITOR, /* monitor */ 875 IWINFO_OPMODE_MESHPOINT, /* mesh point */ 876 IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */ 877 IWINFO_OPMODE_P2P_GO, /* P2P-GO */ 878 }; 879 880 if (tb[NL80211_ATTR_IFTYPE]) 881 *mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])]; 882 883 return NL_SKIP; 884 } 885 886 887 static int nl80211_get_mode(const char *ifname, int *buf) 888 { 889 char *res; 890 891 *buf = IWINFO_OPMODE_UNKNOWN; 892 893 res = nl80211_phy2ifname(ifname); 894 895 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 896 nl80211_get_mode_cb, buf); 897 898 return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0; 899 } 900 901 static int __nl80211_hostapd_query(const char *ifname, ...) 902 { 903 va_list ap, ap_cur; 904 char *phy, *search, *dest, *key, *val, buf[128]; 905 int len, mode, found = 0, match = 1; 906 FILE *fp; 907 908 if (nl80211_get_mode(ifname, &mode)) 909 return 0; 910 911 if (mode != IWINFO_OPMODE_MASTER && mode != IWINFO_OPMODE_AP_VLAN) 912 return 0; 913 914 phy = nl80211_ifname2phy(ifname); 915 916 if (!phy) 917 return 0; 918 919 snprintf(buf, sizeof(buf), "/var/run/hostapd-%s.conf", phy); 920 fp = fopen(buf, "r"); 921 922 if (!fp) 923 return 0; 924 925 va_start(ap, ifname); 926 927 /* clear all destination buffers */ 928 va_copy(ap_cur, ap); 929 930 while ((search = va_arg(ap_cur, char *)) != NULL) 931 { 932 dest = va_arg(ap_cur, char *); 933 len = va_arg(ap_cur, int); 934 935 memset(dest, 0, len); 936 } 937 938 va_end(ap_cur); 939 940 /* iterate applicable lines and copy found values into dest buffers */ 941 while (fgets(buf, sizeof(buf), fp)) 942 { 943 key = strtok(buf, " =\t\n"); 944 val = strtok(NULL, "\n"); 945 946 if (!key || !val || !*key || *key == '#') 947 continue; 948 949 if (!strcmp(key, "interface") || !strcmp(key, "bss")) 950 match = !strcmp(ifname, val); 951 952 if (!match) 953 continue; 954 955 va_copy(ap_cur, ap); 956 957 while ((search = va_arg(ap_cur, char *)) != NULL) 958 { 959 dest = va_arg(ap_cur, char *); 960 len = va_arg(ap_cur, int); 961 962 if (!strcmp(search, key)) 963 { 964 strncpy(dest, val, len - 1); 965 found++; 966 break; 967 } 968 } 969 970 va_end(ap_cur); 971 } 972 973 fclose(fp); 974 975 va_end(ap); 976 977 return found; 978 } 979 980 #define nl80211_hostapd_query(ifname, ...) \ 981 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL) 982 983 984 static inline int nl80211_wpactl_recv(int sock, char *buf, int blen) 985 { 986 fd_set rfds; 987 struct timeval tv = { 0, 256000 }; 988 989 FD_ZERO(&rfds); 990 FD_SET(sock, &rfds); 991 992 memset(buf, 0, blen); 993 994 if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0) 995 return -1; 996 997 if (!FD_ISSET(sock, &rfds)) 998 return -1; 999 1000 return recv(sock, buf, blen - 1, 0); 1001 } 1002 1003 static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local) 1004 { 1005 struct sockaddr_un remote = { 0 }; 1006 size_t remote_length, local_length; 1007 1008 int sock = socket(PF_UNIX, SOCK_DGRAM, 0); 1009 if (sock < 0) 1010 return sock; 1011 1012 remote.sun_family = AF_UNIX; 1013 remote_length = sizeof(remote.sun_family) + 1014 sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname); 1015 1016 /* Set client socket file permissions so that bind() creates the client 1017 * socket with these permissions and there is no need to try to change 1018 * them with chmod() after bind() which would have potential issues with 1019 * race conditions. These permissions are needed to make sure the server 1020 * side (wpa_supplicant or hostapd) can reply to the control interface 1021 * messages. 1022 * 1023 * The lchown() calls below after bind() are also part of the needed 1024 * operations to allow the response to go through. Those are using the 1025 * no-deference-symlinks version to avoid races. */ 1026 fchmod(sock, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 1027 1028 if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0) 1029 { 1030 close(sock); 1031 return -1; 1032 } 1033 1034 if (connect(sock, (struct sockaddr *)&remote, remote_length)) 1035 { 1036 close(sock); 1037 return -1; 1038 } 1039 1040 local->sun_family = AF_UNIX; 1041 local_length = sizeof(local->sun_family) + 1042 sprintf(local->sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid()); 1043 1044 if (bind(sock, (struct sockaddr *)local, local_length) < 0) 1045 { 1046 close(sock); 1047 return -1; 1048 } 1049 1050 /* Set group even if we do not have privileges to change owner */ 1051 lchown(local->sun_path, -1, 101); 1052 lchown(local->sun_path, 101, 101); 1053 1054 return sock; 1055 } 1056 1057 static int __nl80211_wpactl_query(const char *ifname, ...) 1058 { 1059 va_list ap, ap_cur; 1060 struct sockaddr_un local = { 0 }; 1061 int len, mode, found = 0, sock = -1; 1062 char *search, *dest, *key, *val, *line, *pos, buf[512]; 1063 1064 if (nl80211_get_mode(ifname, &mode)) 1065 return 0; 1066 1067 if (mode != IWINFO_OPMODE_CLIENT && 1068 mode != IWINFO_OPMODE_ADHOC && 1069 mode != IWINFO_OPMODE_MESHPOINT) 1070 return 0; 1071 1072 sock = nl80211_wpactl_connect(ifname, &local); 1073 1074 if (sock < 0) 1075 return 0; 1076 1077 va_start(ap, ifname); 1078 1079 /* clear all destination buffers */ 1080 va_copy(ap_cur, ap); 1081 1082 while ((search = va_arg(ap_cur, char *)) != NULL) 1083 { 1084 dest = va_arg(ap_cur, char *); 1085 len = va_arg(ap_cur, int); 1086 1087 memset(dest, 0, len); 1088 } 1089 1090 va_end(ap_cur); 1091 1092 send(sock, "STATUS", 6, 0); 1093 1094 while (true) 1095 { 1096 if (nl80211_wpactl_recv(sock, buf, sizeof(buf)) <= 0) 1097 break; 1098 1099 if (buf[0] == '<') 1100 continue; 1101 1102 for (line = strtok_r(buf, "\n", &pos); 1103 line != NULL; 1104 line = strtok_r(NULL, "\n", &pos)) 1105 { 1106 key = strtok(line, "="); 1107 val = strtok(NULL, "\n"); 1108 1109 if (!key || !val) 1110 continue; 1111 1112 va_copy(ap_cur, ap); 1113 1114 while ((search = va_arg(ap_cur, char *)) != NULL) 1115 { 1116 dest = va_arg(ap_cur, char *); 1117 len = va_arg(ap_cur, int); 1118 1119 if (!strcmp(search, key)) 1120 { 1121 strncpy(dest, val, len - 1); 1122 found++; 1123 break; 1124 } 1125 } 1126 1127 va_end(ap_cur); 1128 } 1129 1130 break; 1131 } 1132 1133 va_end(ap); 1134 1135 close(sock); 1136 unlink(local.sun_path); 1137 1138 return found; 1139 } 1140 1141 #define nl80211_wpactl_query(ifname, ...) \ 1142 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL) 1143 1144 1145 static char * nl80211_ifadd(const char *ifname) 1146 { 1147 char path[PATH_MAX]; 1148 static char nif[IFNAMSIZ] = { 0 }; 1149 struct nl80211_msg_conveyor *req; 1150 FILE *sysfs; 1151 1152 req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0); 1153 if (req) 1154 { 1155 snprintf(nif, sizeof(nif), "tmp.%s", ifname); 1156 1157 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif); 1158 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION); 1159 1160 nl80211_send(req, NULL, NULL); 1161 1162 snprintf(path, sizeof(path) - 1, 1163 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif); 1164 1165 if ((sysfs = fopen(path, "w")) != NULL) 1166 { 1167 fwrite("\n", 1, 2, sysfs); 1168 fclose(sysfs); 1169 } 1170 1171 return nif; 1172 1173 nla_put_failure: 1174 nl80211_free(req); 1175 } 1176 1177 return NULL; 1178 } 1179 1180 static void nl80211_ifdel(const char *ifname) 1181 { 1182 struct nl80211_msg_conveyor *req; 1183 1184 req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0); 1185 if (req) 1186 { 1187 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname); 1188 1189 nl80211_send(req, NULL, NULL); 1190 return; 1191 1192 nla_put_failure: 1193 nl80211_free(req); 1194 } 1195 } 1196 1197 static void nl80211_hostapd_hup(const char *ifname) 1198 { 1199 int fd, pid = 0; 1200 char buf[32]; 1201 char *phy = nl80211_ifname2phy(ifname); 1202 1203 if (phy) 1204 { 1205 snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy); 1206 if ((fd = open(buf, O_RDONLY)) >= 0) 1207 { 1208 if (read(fd, buf, sizeof(buf)) > 0) 1209 pid = atoi(buf); 1210 1211 close(fd); 1212 } 1213 1214 if (pid > 0) 1215 kill(pid, 1); 1216 } 1217 } 1218 1219 1220 static int nl80211_probe(const char *ifname) 1221 { 1222 return !!nl80211_ifname2phy(ifname); 1223 } 1224 1225 struct nl80211_ssid_bssid { 1226 unsigned char *ssid; 1227 unsigned char bssid[7]; 1228 }; 1229 1230 static int nl80211_get_macaddr_cb(struct nl_msg *msg, void *arg) 1231 { 1232 struct nl80211_ssid_bssid *sb = arg; 1233 struct nlattr **tb = nl80211_parse(msg); 1234 1235 if (tb[NL80211_ATTR_MAC]) { 1236 sb->bssid[0] = 1; 1237 memcpy(sb->bssid + 1, nla_data(tb[NL80211_ATTR_MAC]), 1238 sizeof(sb->bssid) - 1); 1239 } 1240 1241 return NL_SKIP; 1242 } 1243 1244 static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg) 1245 { 1246 int ielen; 1247 unsigned char *ie; 1248 struct nl80211_ssid_bssid *sb = arg; 1249 struct nlattr **tb = nl80211_parse(msg); 1250 struct nlattr *bss[NL80211_BSS_MAX + 1]; 1251 1252 static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 1253 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 }, 1254 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 1255 }; 1256 1257 if (!tb[NL80211_ATTR_BSS] || 1258 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], 1259 bss_policy) || 1260 !bss[NL80211_BSS_BSSID] || 1261 !bss[NL80211_BSS_STATUS] || 1262 !bss[NL80211_BSS_INFORMATION_ELEMENTS]) 1263 { 1264 return NL_SKIP; 1265 } 1266 1267 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) 1268 { 1269 case NL80211_BSS_STATUS_ASSOCIATED: 1270 case NL80211_BSS_STATUS_AUTHENTICATED: 1271 case NL80211_BSS_STATUS_IBSS_JOINED: 1272 1273 if (sb->ssid) 1274 { 1275 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 1276 ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 1277 1278 while (ielen >= 2 && ielen >= ie[1]) 1279 { 1280 if (ie[0] == 0) 1281 { 1282 memcpy(sb->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE)); 1283 return NL_SKIP; 1284 } 1285 1286 ielen -= ie[1] + 2; 1287 ie += ie[1] + 2; 1288 } 1289 } 1290 else 1291 { 1292 sb->bssid[0] = 1; 1293 memcpy(sb->bssid + 1, nla_data(bss[NL80211_BSS_BSSID]), 6); 1294 return NL_SKIP; 1295 } 1296 1297 default: 1298 return NL_SKIP; 1299 } 1300 } 1301 1302 static int nl80211_get_ssid(const char *ifname, char *buf) 1303 { 1304 char *res; 1305 struct nl80211_ssid_bssid sb = { .ssid = (unsigned char *)buf }; 1306 1307 /* try to find ssid from scan dump results */ 1308 res = nl80211_phy2ifname(ifname); 1309 sb.ssid[0] = 0; 1310 1311 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, 1312 nl80211_get_ssid_bssid_cb, &sb); 1313 1314 /* failed, try to find from hostapd info */ 1315 if (sb.ssid[0] == 0) 1316 nl80211_hostapd_query(ifname, "ssid", sb.ssid, 1317 IWINFO_ESSID_MAX_SIZE + 1); 1318 1319 /* failed, try to obtain Mesh ID */ 1320 if (sb.ssid[0] == 0) 1321 iwinfo_ubus_query(res ? res : ifname, "mesh_id", 1322 buf, IWINFO_ESSID_MAX_SIZE + 1); 1323 1324 return (sb.ssid[0] == 0) ? -1 : 0; 1325 } 1326 1327 static int nl80211_get_bssid(const char *ifname, char *buf) 1328 { 1329 char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")]; 1330 struct nl80211_ssid_bssid sb = { }; 1331 1332 res = nl80211_phy2ifname(ifname); 1333 1334 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */ 1335 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1336 nl80211_get_macaddr_cb, &sb); 1337 1338 /* failed, try to find bssid from scan dump results */ 1339 if (sb.bssid[0] == 0) 1340 nl80211_request(res ? res : ifname, 1341 NL80211_CMD_GET_SCAN, NLM_F_DUMP, 1342 nl80211_get_ssid_bssid_cb, &sb); 1343 1344 /* failed, try to find mac from hostapd info */ 1345 if ((sb.bssid[0] == 0) && 1346 nl80211_hostapd_query(ifname, "bssid", bssid, sizeof(bssid))) 1347 { 1348 sb.bssid[0] = 1; 1349 sb.bssid[1] = strtol(&bssid[0], NULL, 16); 1350 sb.bssid[2] = strtol(&bssid[3], NULL, 16); 1351 sb.bssid[3] = strtol(&bssid[6], NULL, 16); 1352 sb.bssid[4] = strtol(&bssid[9], NULL, 16); 1353 sb.bssid[5] = strtol(&bssid[12], NULL, 16); 1354 sb.bssid[6] = strtol(&bssid[15], NULL, 16); 1355 } 1356 1357 if (sb.bssid[0]) 1358 { 1359 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", 1360 sb.bssid[1], sb.bssid[2], sb.bssid[3], 1361 sb.bssid[4], sb.bssid[5], sb.bssid[6]); 1362 1363 return 0; 1364 } 1365 1366 return -1; 1367 } 1368 1369 1370 static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg) 1371 { 1372 int *freq = arg; 1373 struct nlattr **attr = nl80211_parse(msg); 1374 struct nlattr *binfo[NL80211_BSS_MAX + 1]; 1375 1376 static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 1377 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, 1378 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 1379 }; 1380 1381 if (attr[NL80211_ATTR_BSS] && 1382 !nla_parse_nested(binfo, NL80211_BSS_MAX, 1383 attr[NL80211_ATTR_BSS], bss_policy)) 1384 { 1385 if (binfo[NL80211_BSS_STATUS] && binfo[NL80211_BSS_FREQUENCY]) 1386 *freq = nla_get_u32(binfo[NL80211_BSS_FREQUENCY]); 1387 } 1388 1389 return NL_SKIP; 1390 } 1391 1392 static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg) 1393 { 1394 int *freq = arg; 1395 struct nlattr **tb = nl80211_parse(msg); 1396 1397 if (tb[NL80211_ATTR_WIPHY_FREQ]) 1398 *freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]); 1399 1400 return NL_SKIP; 1401 } 1402 1403 static int nl80211_get_frequency(const char *ifname, int *buf) 1404 { 1405 char *res, channel[4] = { 0 }, hwmode[3] = { 0 }, ax[2] = { 0 }; 1406 1407 /* try to find frequency from interface info */ 1408 res = nl80211_phy2ifname(ifname); 1409 *buf = 0; 1410 1411 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1412 nl80211_get_frequency_info_cb, buf); 1413 1414 /* failed, try to find frequency from hostapd info */ 1415 if ((*buf == 0) && 1416 nl80211_hostapd_query(ifname, "hw_mode", hwmode, sizeof(hwmode), 1417 "channel", channel, sizeof(channel), 1418 "ieee80211ax", ax, sizeof(ax)) >= 2) 1419 { 1420 *buf = nl80211_channel2freq(atoi(channel), hwmode, ax[0] == '1'); 1421 } 1422 1423 /* failed, try to find frequency from scan results */ 1424 if (*buf == 0) 1425 { 1426 res = nl80211_phy2ifname(ifname); 1427 1428 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, 1429 nl80211_get_frequency_scan_cb, buf); 1430 } 1431 1432 return (*buf == 0) ? -1 : 0; 1433 } 1434 1435 static int nl80211_get_center_freq1_cb(struct nl_msg *msg, void *arg) 1436 { 1437 int *freq = arg; 1438 struct nlattr **tb = nl80211_parse(msg); 1439 1440 if (tb[NL80211_ATTR_CENTER_FREQ1]) 1441 *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]); 1442 1443 return NL_SKIP; 1444 } 1445 1446 static int nl80211_get_center_freq1(const char *ifname, int *buf) 1447 { 1448 char *res; 1449 1450 /* try to find frequency from interface info */ 1451 res = nl80211_phy2ifname(ifname); 1452 *buf = 0; 1453 1454 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1455 nl80211_get_center_freq1_cb, buf); 1456 1457 return (*buf == 0) ? -1 : 0; 1458 } 1459 1460 static int nl80211_get_center_freq2_cb(struct nl_msg *msg, void *arg) 1461 { 1462 int *freq = arg; 1463 struct nlattr **tb = nl80211_parse(msg); 1464 1465 if (tb[NL80211_ATTR_CENTER_FREQ2]) 1466 *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]); 1467 1468 return NL_SKIP; 1469 } 1470 1471 static int nl80211_get_center_freq2(const char *ifname, int *buf) 1472 { 1473 char *res; 1474 1475 /* try to find frequency from interface info */ 1476 res = nl80211_phy2ifname(ifname); 1477 *buf = 0; 1478 1479 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1480 nl80211_get_center_freq2_cb, buf); 1481 1482 return (*buf == 0) ? -1 : 0; 1483 } 1484 1485 static int nl80211_get_channel(const char *ifname, int *buf) 1486 { 1487 if (!nl80211_get_frequency(ifname, buf)) 1488 { 1489 *buf = nl80211_freq2channel(*buf); 1490 return 0; 1491 } 1492 1493 return -1; 1494 } 1495 1496 static int nl80211_get_center_chan1(const char *ifname, int *buf) 1497 { 1498 if (!nl80211_get_center_freq1(ifname, buf)) 1499 { 1500 *buf = nl80211_freq2channel(*buf); 1501 return 0; 1502 } 1503 1504 return -1; 1505 } 1506 1507 static int nl80211_get_center_chan2(const char *ifname, int *buf) 1508 { 1509 if (!nl80211_get_center_freq2(ifname, buf)) 1510 { 1511 *buf = nl80211_freq2channel(*buf); 1512 return 0; 1513 } 1514 1515 return -1; 1516 } 1517 1518 static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg) 1519 { 1520 int *buf = arg; 1521 struct nlattr **tb = nl80211_parse(msg); 1522 1523 if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]) 1524 *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])); 1525 1526 return NL_SKIP; 1527 } 1528 1529 static int nl80211_get_txpower(const char *ifname, int *buf) 1530 { 1531 char *res; 1532 1533 res = nl80211_phy2ifname(ifname); 1534 *buf = 0; 1535 1536 if (nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1537 nl80211_get_txpower_cb, buf)) 1538 return -1; 1539 1540 return 0; 1541 } 1542 1543 1544 static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg) 1545 { 1546 int8_t dbm; 1547 int16_t mbit; 1548 struct nl80211_rssi_rate *rr = arg; 1549 struct nlattr **attr = nl80211_parse(msg); 1550 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; 1551 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; 1552 1553 static const struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { 1554 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, 1555 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, 1556 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, 1557 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 }, 1558 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 }, 1559 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, 1560 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, 1561 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, 1562 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, 1563 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, 1564 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, 1565 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, 1566 }; 1567 1568 static const struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { 1569 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, 1570 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, 1571 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, 1572 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, 1573 }; 1574 1575 if (attr[NL80211_ATTR_STA_INFO]) 1576 { 1577 if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, 1578 attr[NL80211_ATTR_STA_INFO], stats_policy)) 1579 { 1580 if (sinfo[NL80211_STA_INFO_SIGNAL]) 1581 { 1582 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); 1583 rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1); 1584 rr->rssi_samples++; 1585 } 1586 1587 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) 1588 { 1589 if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 1590 sinfo[NL80211_STA_INFO_TX_BITRATE], 1591 rate_policy)) 1592 { 1593 if (rinfo[NL80211_RATE_INFO_BITRATE]) 1594 { 1595 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]); 1596 rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1); 1597 rr->rate_samples++; 1598 } 1599 } 1600 } 1601 } 1602 } 1603 1604 return NL_SKIP; 1605 } 1606 1607 static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r) 1608 { 1609 DIR *d; 1610 struct dirent *de; 1611 1612 memset(r, 0, sizeof(*r)); 1613 1614 if ((d = opendir("/sys/class/net")) != NULL) 1615 { 1616 while ((de = readdir(d)) != NULL) 1617 { 1618 if (!strncmp(de->d_name, ifname, strlen(ifname)) && 1619 (!de->d_name[strlen(ifname)] || 1620 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) 1621 { 1622 nl80211_request(de->d_name, NL80211_CMD_GET_STATION, 1623 NLM_F_DUMP, nl80211_fill_signal_cb, r); 1624 } 1625 } 1626 1627 closedir(d); 1628 } 1629 } 1630 1631 static int nl80211_get_bitrate(const char *ifname, int *buf) 1632 { 1633 struct nl80211_rssi_rate rr; 1634 1635 nl80211_fill_signal(ifname, &rr); 1636 1637 if (rr.rate_samples) 1638 { 1639 *buf = (rr.rate * 100); 1640 return 0; 1641 } 1642 1643 return -1; 1644 } 1645 1646 static int nl80211_get_signal(const char *ifname, int *buf) 1647 { 1648 struct nl80211_rssi_rate rr; 1649 1650 nl80211_fill_signal(ifname, &rr); 1651 1652 if (rr.rssi_samples) 1653 { 1654 *buf = rr.rssi; 1655 return 0; 1656 } 1657 1658 return -1; 1659 } 1660 1661 static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg) 1662 { 1663 int8_t *noise = arg; 1664 struct nlattr **tb = nl80211_parse(msg); 1665 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1]; 1666 1667 static const struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = { 1668 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 1669 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 1670 }; 1671 1672 if (!tb[NL80211_ATTR_SURVEY_INFO]) 1673 return NL_SKIP; 1674 1675 if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX, 1676 tb[NL80211_ATTR_SURVEY_INFO], sp)) 1677 return NL_SKIP; 1678 1679 if (!si[NL80211_SURVEY_INFO_NOISE]) 1680 return NL_SKIP; 1681 1682 if (!*noise || si[NL80211_SURVEY_INFO_IN_USE]) 1683 *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]); 1684 1685 return NL_SKIP; 1686 } 1687 1688 1689 static int nl80211_get_noise(const char *ifname, int *buf) 1690 { 1691 int8_t noise = 0; 1692 1693 if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP, 1694 nl80211_get_noise_cb, &noise)) 1695 goto out; 1696 1697 *buf = noise; 1698 return 0; 1699 1700 out: 1701 *buf = 0; 1702 return -1; 1703 } 1704 1705 static int nl80211_get_quality(const char *ifname, int *buf) 1706 { 1707 int signal; 1708 1709 if (!nl80211_get_signal(ifname, &signal)) 1710 { 1711 /* A positive signal level is usually just a quality 1712 * value, pass through as-is */ 1713 if (signal >= 0) 1714 { 1715 *buf = signal; 1716 } 1717 1718 /* The cfg80211 wext compat layer assumes a signal range 1719 * of -110 dBm to -40 dBm, the quality value is derived 1720 * by adding 110 to the signal level */ 1721 else 1722 { 1723 if (signal < -110) 1724 signal = -110; 1725 else if (signal > -40) 1726 signal = -40; 1727 1728 *buf = (signal + 110); 1729 } 1730 1731 return 0; 1732 } 1733 1734 return -1; 1735 } 1736 1737 static int nl80211_get_quality_max(const char *ifname, int *buf) 1738 { 1739 /* The cfg80211 wext compat layer assumes a maximum 1740 * quality of 70 */ 1741 *buf = 70; 1742 1743 return 0; 1744 } 1745 1746 static int nl80211_check_wepkey(const char *key) 1747 { 1748 if (key && *key) 1749 { 1750 switch (strlen(key)) 1751 { 1752 case 5: 1753 case 10: 1754 return IWINFO_CIPHER_WEP40; 1755 1756 case 13: 1757 case 26: 1758 return IWINFO_CIPHER_WEP104; 1759 } 1760 } 1761 1762 return 0; 1763 } 1764 1765 static const struct { 1766 const char *match; 1767 int version; 1768 int suite; 1769 } wpa_key_mgmt_strings[] = { 1770 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x }, 1771 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x }, 1772 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x }, 1773 { "EAP-SHA384", 4, IWINFO_KMGMT_8021x }, 1774 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x }, 1775 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK }, 1776 { "NONE", 0, IWINFO_KMGMT_NONE }, 1777 { "None", 0, IWINFO_KMGMT_NONE }, 1778 { "PSK", 0, IWINFO_KMGMT_PSK }, 1779 { "EAP", 0, IWINFO_KMGMT_8021x }, 1780 { "SAE", 4, IWINFO_KMGMT_SAE }, 1781 { "OWE", 4, IWINFO_KMGMT_OWE } 1782 }; 1783 1784 static void parse_wpa_suites(const char *str, int defversion, 1785 uint8_t *versions, uint8_t *suites) 1786 { 1787 size_t l; 1788 int i, version; 1789 const char *p, *q, *m, *sep = " \t\n,-+/"; 1790 1791 for (p = str; *p; ) 1792 { 1793 q = p; 1794 1795 for (i = 0; i < ARRAY_SIZE(wpa_key_mgmt_strings); i++) 1796 { 1797 m = wpa_key_mgmt_strings[i].match; 1798 l = strlen(m); 1799 1800 if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l]))) 1801 { 1802 if (wpa_key_mgmt_strings[i].version != 0) 1803 version = wpa_key_mgmt_strings[i].version; 1804 else 1805 version = defversion; 1806 1807 *versions |= version; 1808 *suites |= wpa_key_mgmt_strings[i].suite; 1809 1810 q += l; 1811 break; 1812 } 1813 } 1814 1815 if (q == p) 1816 q += strcspn(q, sep); 1817 1818 p = q + strspn(q, sep); 1819 } 1820 } 1821 1822 static const struct { 1823 const char *match; 1824 int cipher; 1825 } wpa_cipher_strings[] = { 1826 { "WEP-104", IWINFO_CIPHER_WEP104 }, 1827 { "WEP-40", IWINFO_CIPHER_WEP40 }, 1828 { "NONE", IWINFO_CIPHER_NONE }, 1829 { "TKIP", IWINFO_CIPHER_TKIP }, 1830 { "CCMP-256",IWINFO_CIPHER_CCMP256 }, 1831 { "CCMP", IWINFO_CIPHER_CCMP }, 1832 { "GCMP-256",IWINFO_CIPHER_GCMP256 }, 1833 { "GCMP", IWINFO_CIPHER_GCMP } 1834 }; 1835 1836 static void parse_wpa_ciphers(const char *str, uint16_t *ciphers) 1837 { 1838 int i; 1839 size_t l; 1840 const char *m, *p, *q, *sep = " \t\n,-+/"; 1841 1842 for (p = str; *p; ) 1843 { 1844 q = p; 1845 1846 for (i = 0; i < ARRAY_SIZE(wpa_cipher_strings); i++) 1847 { 1848 m = wpa_cipher_strings[i].match; 1849 l = strlen(m); 1850 1851 if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l]))) 1852 { 1853 *ciphers |= wpa_cipher_strings[i].cipher; 1854 1855 q += l; 1856 break; 1857 } 1858 } 1859 1860 if (q == p) 1861 q += strcspn(q, sep); 1862 1863 p = q + strspn(q, sep); 1864 } 1865 } 1866 1867 static int nl80211_get_encryption(const char *ifname, char *buf) 1868 { 1869 char *p; 1870 int opmode; 1871 uint8_t wpa_version = 0; 1872 char wpa[2], wpa_key_mgmt[64], wpa_pairwise[16], wpa_groupwise[16]; 1873 char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27]; 1874 char mode[16]; 1875 1876 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf; 1877 1878 /* WPA supplicant */ 1879 if (nl80211_wpactl_query(ifname, 1880 "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise), 1881 "group_cipher", wpa_groupwise, sizeof(wpa_groupwise), 1882 "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt), 1883 "mode", mode, sizeof(mode))) 1884 { 1885 /* WEP or Open */ 1886 if (!strcmp(wpa_key_mgmt, "NONE")) 1887 { 1888 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); 1889 parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers); 1890 1891 if (c->pair_ciphers != 0 && c->pair_ciphers != IWINFO_CIPHER_NONE) { 1892 c->enabled = 1; 1893 c->auth_suites = IWINFO_KMGMT_NONE; 1894 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; 1895 } 1896 else { 1897 c->pair_ciphers = 0; 1898 c->group_ciphers = 0; 1899 } 1900 } 1901 1902 /* MESH with SAE */ 1903 else if (!strcmp(mode, "mesh") && !strcmp(wpa_key_mgmt, "UNKNOWN")) 1904 { 1905 c->enabled = 1; 1906 c->wpa_version = 4; 1907 c->auth_suites = IWINFO_KMGMT_SAE; 1908 c->pair_ciphers = IWINFO_CIPHER_CCMP; 1909 c->group_ciphers = IWINFO_CIPHER_CCMP; 1910 } 1911 1912 /* WPA */ 1913 else 1914 { 1915 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); 1916 parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers); 1917 1918 p = wpa_key_mgmt; 1919 1920 if (!strncmp(p, "WPA2-", 5) || !strncmp(p, "WPA2/", 5)) 1921 { 1922 p += 5; 1923 wpa_version = 2; 1924 } 1925 else if (!strncmp(p, "WPA-", 4)) 1926 { 1927 p += 4; 1928 wpa_version = 1; 1929 } 1930 1931 parse_wpa_suites(p, wpa_version, &c->wpa_version, &c->auth_suites); 1932 1933 c->enabled = !!(c->wpa_version && c->auth_suites); 1934 } 1935 1936 return 0; 1937 } 1938 1939 /* Hostapd */ 1940 else if (nl80211_hostapd_query(ifname, 1941 "wpa", wpa, sizeof(wpa), 1942 "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt), 1943 "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise), 1944 "auth_algs", auth_algs, sizeof(auth_algs), 1945 "wep_key0", wep_key0, sizeof(wep_key0), 1946 "wep_key1", wep_key1, sizeof(wep_key1), 1947 "wep_key2", wep_key2, sizeof(wep_key2), 1948 "wep_key3", wep_key3, sizeof(wep_key3))) 1949 { 1950 c->wpa_version = 0; 1951 1952 if (wpa_key_mgmt[0]) 1953 { 1954 for (p = strtok(wpa_key_mgmt, " \t"); p != NULL; p = strtok(NULL, " \t")) 1955 { 1956 if (!strncmp(p, "WPA-", 4)) 1957 p += 4; 1958 1959 if (!strncmp(p, "FT-", 3)) 1960 p += 3; 1961 1962 parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites); 1963 } 1964 1965 c->enabled = c->wpa_version ? 1 : 0; 1966 } 1967 1968 if (wpa_pairwise[0]) 1969 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); 1970 1971 if (auth_algs[0]) 1972 { 1973 switch (atoi(auth_algs)) 1974 { 1975 case 1: 1976 c->auth_algs |= IWINFO_AUTH_OPEN; 1977 break; 1978 1979 case 2: 1980 c->auth_algs |= IWINFO_AUTH_SHARED; 1981 break; 1982 1983 case 3: 1984 c->auth_algs |= IWINFO_AUTH_OPEN; 1985 c->auth_algs |= IWINFO_AUTH_SHARED; 1986 break; 1987 } 1988 1989 c->pair_ciphers |= nl80211_check_wepkey(wep_key0); 1990 c->pair_ciphers |= nl80211_check_wepkey(wep_key1); 1991 c->pair_ciphers |= nl80211_check_wepkey(wep_key2); 1992 c->pair_ciphers |= nl80211_check_wepkey(wep_key3); 1993 1994 c->enabled = (c->auth_algs && c->pair_ciphers) ? 1 : 0; 1995 } 1996 1997 c->group_ciphers = c->pair_ciphers; 1998 1999 return 0; 2000 } 2001 2002 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */ 2003 else if (!nl80211_get_mode(ifname, &opmode) && 2004 (opmode == IWINFO_OPMODE_ADHOC || 2005 opmode == IWINFO_OPMODE_MESHPOINT)) 2006 { 2007 c->enabled = 0; 2008 2009 return 0; 2010 } 2011 2012 2013 return -1; 2014 } 2015 2016 static int nl80211_get_phyname(const char *ifname, char *buf) 2017 { 2018 const char *name; 2019 2020 name = nl80211_ifname2phy(ifname); 2021 2022 if (name) 2023 { 2024 strcpy(buf, name); 2025 return 0; 2026 } 2027 else if ((name = nl80211_phy2ifname(ifname)) != NULL) 2028 { 2029 name = nl80211_ifname2phy(name); 2030 2031 if (name) 2032 { 2033 strcpy(buf, ifname); 2034 return 0; 2035 } 2036 } 2037 2038 return -1; 2039 } 2040 2041 2042 static void nl80211_parse_rateinfo(struct nlattr **ri, 2043 struct iwinfo_rate_entry *re) 2044 { 2045 if (ri[NL80211_RATE_INFO_BITRATE32]) 2046 re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100; 2047 else if (ri[NL80211_RATE_INFO_BITRATE]) 2048 re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100; 2049 2050 if (ri[NL80211_RATE_INFO_EHT_MCS]) 2051 { 2052 re->is_eht = 1; 2053 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_EHT_MCS]); 2054 2055 if (ri[NL80211_RATE_INFO_EHT_NSS]) 2056 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_EHT_NSS]); 2057 if (ri[NL80211_RATE_INFO_EHT_GI]) 2058 re->eht_gi = nla_get_u8(ri[NL80211_RATE_INFO_EHT_GI]); 2059 } 2060 else if (ri[NL80211_RATE_INFO_HE_MCS]) 2061 { 2062 re->is_he = 1; 2063 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_HE_MCS]); 2064 2065 if (ri[NL80211_RATE_INFO_HE_NSS]) 2066 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_HE_NSS]); 2067 if (ri[NL80211_RATE_INFO_HE_GI]) 2068 re->he_gi = nla_get_u8(ri[NL80211_RATE_INFO_HE_GI]); 2069 if (ri[NL80211_RATE_INFO_HE_DCM]) 2070 re->he_dcm = nla_get_u8(ri[NL80211_RATE_INFO_HE_DCM]); 2071 } 2072 else if (ri[NL80211_RATE_INFO_VHT_MCS]) 2073 { 2074 re->is_vht = 1; 2075 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]); 2076 2077 if (ri[NL80211_RATE_INFO_VHT_NSS]) 2078 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]); 2079 } 2080 else if (ri[NL80211_RATE_INFO_MCS]) 2081 { 2082 re->is_ht = 1; 2083 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]); 2084 } 2085 2086 if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH]) 2087 re->mhz = 5; 2088 else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH]) 2089 re->mhz = 10; 2090 else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH]) 2091 re->mhz = 40; 2092 else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH]) 2093 re->mhz = 80; 2094 else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] || 2095 ri[NL80211_RATE_INFO_160_MHZ_WIDTH]) 2096 re->mhz = 160; 2097 else if (ri[NL80211_RATE_INFO_320_MHZ_WIDTH]) 2098 re->mhz_hi = 320 / 256, re->mhz = 320 % 256; 2099 else 2100 re->mhz = 20; 2101 2102 if (ri[NL80211_RATE_INFO_SHORT_GI]) 2103 re->is_short_gi = 1; 2104 2105 re->is_40mhz = (re->mhz == 40); 2106 } 2107 2108 static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg) 2109 { 2110 struct nl80211_array_buf *arr = arg; 2111 struct iwinfo_survey_entry *e = arr->buf; 2112 struct nlattr **attr = nl80211_parse(msg); 2113 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; 2114 int rc; 2115 2116 static const struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { 2117 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 2118 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 2119 [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64 }, 2120 [NL80211_SURVEY_INFO_TIME_BUSY] = { .type = NLA_U64 }, 2121 [NL80211_SURVEY_INFO_TIME_EXT_BUSY] = { .type = NLA_U64 }, 2122 [NL80211_SURVEY_INFO_TIME_RX] = { .type = NLA_U64 }, 2123 [NL80211_SURVEY_INFO_TIME_TX] = { .type = NLA_U64 }, 2124 }; 2125 2126 rc = nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, 2127 attr[NL80211_ATTR_SURVEY_INFO], 2128 survey_policy); 2129 if (rc) 2130 return NL_SKIP; 2131 2132 /* advance to end of array */ 2133 e += arr->count; 2134 memset(e, 0, sizeof(*e)); 2135 2136 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY]) 2137 e->mhz = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]); 2138 2139 if (sinfo[NL80211_SURVEY_INFO_NOISE]) 2140 e->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); 2141 2142 if (sinfo[NL80211_SURVEY_INFO_TIME]) 2143 e->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]); 2144 2145 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY]) 2146 e->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]); 2147 2148 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]) 2149 e->busy_time_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]); 2150 2151 if (sinfo[NL80211_SURVEY_INFO_TIME_RX]) 2152 e->rxtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]); 2153 2154 if (sinfo[NL80211_SURVEY_INFO_TIME_TX]) 2155 e->txtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]); 2156 2157 arr->count++; 2158 return NL_SKIP; 2159 } 2160 2161 2162 static void plink_state_to_str(char *dst, unsigned state) 2163 { 2164 switch (state) { 2165 case NL80211_PLINK_LISTEN: 2166 strcpy(dst, "LISTEN"); 2167 break; 2168 case NL80211_PLINK_OPN_SNT: 2169 strcpy(dst, "OPN_SNT"); 2170 break; 2171 case NL80211_PLINK_OPN_RCVD: 2172 strcpy(dst, "OPN_RCVD"); 2173 break; 2174 case NL80211_PLINK_CNF_RCVD: 2175 strcpy(dst, "CNF_RCVD"); 2176 break; 2177 case NL80211_PLINK_ESTAB: 2178 strcpy(dst, "ESTAB"); 2179 break; 2180 case NL80211_PLINK_HOLDING: 2181 strcpy(dst, "HOLDING"); 2182 break; 2183 case NL80211_PLINK_BLOCKED: 2184 strcpy(dst, "BLOCKED"); 2185 break; 2186 default: 2187 strcpy(dst, "UNKNOWN"); 2188 break; 2189 } 2190 } 2191 2192 static void power_mode_to_str(char *dst, struct nlattr *a) 2193 { 2194 enum nl80211_mesh_power_mode pm = nla_get_u32(a); 2195 2196 switch (pm) { 2197 case NL80211_MESH_POWER_ACTIVE: 2198 strcpy(dst, "ACTIVE"); 2199 break; 2200 case NL80211_MESH_POWER_LIGHT_SLEEP: 2201 strcpy(dst, "LIGHT SLEEP"); 2202 break; 2203 case NL80211_MESH_POWER_DEEP_SLEEP: 2204 strcpy(dst, "DEEP SLEEP"); 2205 break; 2206 default: 2207 strcpy(dst, "UNKNOWN"); 2208 break; 2209 } 2210 } 2211 2212 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) 2213 { 2214 struct nl80211_array_buf *arr = arg; 2215 struct iwinfo_assoclist_entry *e = arr->buf; 2216 struct nlattr **attr = nl80211_parse(msg); 2217 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; 2218 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; 2219 struct nl80211_sta_flag_update *sta_flags; 2220 2221 static const struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { 2222 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, 2223 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, 2224 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, 2225 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED }, 2226 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, 2227 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, 2228 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 }, 2229 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, 2230 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, 2231 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 }, 2232 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 }, 2233 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 }, 2234 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 }, 2235 [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32 }, 2236 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 }, 2237 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 }, 2238 [NL80211_STA_INFO_STA_FLAGS] = 2239 { .minlen = sizeof(struct nl80211_sta_flag_update) }, 2240 [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 }, 2241 /* mesh */ 2242 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, 2243 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, 2244 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, 2245 [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32 }, 2246 [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32 }, 2247 [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32 }, 2248 }; 2249 2250 static const struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { 2251 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, 2252 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, 2253 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, 2254 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, 2255 }; 2256 2257 /* advance to end of array */ 2258 e += arr->count; 2259 memset(e, 0, sizeof(*e)); 2260 2261 if (attr[NL80211_ATTR_MAC]) 2262 memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6); 2263 2264 if (attr[NL80211_ATTR_STA_INFO] && 2265 !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, 2266 attr[NL80211_ATTR_STA_INFO], stats_policy)) 2267 { 2268 if (sinfo[NL80211_STA_INFO_SIGNAL]) 2269 e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); 2270 2271 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG]) 2272 e->signal_avg = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]); 2273 2274 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME]) 2275 e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]); 2276 2277 if (sinfo[NL80211_STA_INFO_CONNECTED_TIME]) 2278 e->connected_time = nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]); 2279 2280 if (sinfo[NL80211_STA_INFO_RX_PACKETS]) 2281 e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]); 2282 2283 if (sinfo[NL80211_STA_INFO_TX_PACKETS]) 2284 e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]); 2285 2286 if (sinfo[NL80211_STA_INFO_RX_BITRATE] && 2287 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 2288 sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) 2289 nl80211_parse_rateinfo(rinfo, &e->rx_rate); 2290 2291 if (sinfo[NL80211_STA_INFO_TX_BITRATE] && 2292 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 2293 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) 2294 nl80211_parse_rateinfo(rinfo, &e->tx_rate); 2295 2296 if (sinfo[NL80211_STA_INFO_RX_BYTES64]) 2297 e->rx_bytes = nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]); 2298 else if (sinfo[NL80211_STA_INFO_RX_BYTES]) 2299 e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]); 2300 2301 if (sinfo[NL80211_STA_INFO_TX_BYTES64]) 2302 e->tx_bytes = nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]); 2303 else if (sinfo[NL80211_STA_INFO_TX_BYTES]) 2304 e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]); 2305 2306 if (sinfo[NL80211_STA_INFO_TX_RETRIES]) 2307 e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]); 2308 2309 if (sinfo[NL80211_STA_INFO_TX_FAILED]) 2310 e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]); 2311 2312 if (sinfo[NL80211_STA_INFO_T_OFFSET]) 2313 e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]); 2314 2315 if (sinfo[NL80211_STA_INFO_RX_DROP_MISC]) 2316 e->rx_drop_misc = nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]); 2317 2318 if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]) 2319 e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]); 2320 2321 /* mesh */ 2322 if (sinfo[NL80211_STA_INFO_LLID]) 2323 e->llid = nla_get_u16(sinfo[NL80211_STA_INFO_LLID]); 2324 2325 if (sinfo[NL80211_STA_INFO_PLID]) 2326 e->plid = nla_get_u16(sinfo[NL80211_STA_INFO_PLID]); 2327 2328 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) 2329 plink_state_to_str(e->plink_state, 2330 nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])); 2331 2332 if (sinfo[NL80211_STA_INFO_LOCAL_PM]) 2333 power_mode_to_str(e->local_ps, sinfo[NL80211_STA_INFO_LOCAL_PM]); 2334 if (sinfo[NL80211_STA_INFO_PEER_PM]) 2335 power_mode_to_str(e->peer_ps, sinfo[NL80211_STA_INFO_PEER_PM]); 2336 if (sinfo[NL80211_STA_INFO_NONPEER_PM]) 2337 power_mode_to_str(e->nonpeer_ps, sinfo[NL80211_STA_INFO_NONPEER_PM]); 2338 2339 /* Station flags */ 2340 if (sinfo[NL80211_STA_INFO_STA_FLAGS]) 2341 { 2342 sta_flags = (struct nl80211_sta_flag_update *) 2343 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]); 2344 2345 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) && 2346 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED)) 2347 e->is_authorized = 1; 2348 2349 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) && 2350 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) 2351 e->is_authenticated = 1; 2352 2353 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) && 2354 sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) 2355 e->is_preamble_short = 1; 2356 2357 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) && 2358 sta_flags->set & BIT(NL80211_STA_FLAG_WME)) 2359 e->is_wme = 1; 2360 2361 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) && 2362 sta_flags->set & BIT(NL80211_STA_FLAG_MFP)) 2363 e->is_mfp = 1; 2364 2365 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) && 2366 sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER)) 2367 e->is_tdls = 1; 2368 } 2369 } 2370 2371 e->noise = 0; /* filled in by caller */ 2372 arr->count++; 2373 2374 return NL_SKIP; 2375 } 2376 2377 static int nl80211_get_survey(const char *ifname, char *buf, int *len) 2378 { 2379 struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; 2380 int rc; 2381 2382 rc = nl80211_request(ifname, NL80211_CMD_GET_SURVEY, 2383 NLM_F_DUMP, nl80211_get_survey_cb, &arr); 2384 if (!rc) 2385 *len = (arr.count * sizeof(struct iwinfo_survey_entry)); 2386 else 2387 *len = 0; 2388 2389 return 0; 2390 } 2391 2392 static int nl80211_get_assoclist(const char *ifname, char *buf, int *len) 2393 { 2394 DIR *d; 2395 int i, noise = 0; 2396 struct dirent *de; 2397 struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; 2398 struct iwinfo_assoclist_entry *e; 2399 2400 if ((d = opendir("/sys/class/net")) != NULL) 2401 { 2402 while ((de = readdir(d)) != NULL) 2403 { 2404 if (!strncmp(de->d_name, ifname, strlen(ifname)) && 2405 (!de->d_name[strlen(ifname)] || 2406 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) 2407 { 2408 nl80211_request(de->d_name, NL80211_CMD_GET_STATION, 2409 NLM_F_DUMP, nl80211_get_assoclist_cb, &arr); 2410 } 2411 } 2412 2413 closedir(d); 2414 2415 if (!nl80211_get_noise(ifname, &noise)) 2416 for (i = 0, e = arr.buf; i < arr.count; i++, e++) 2417 e->noise = noise; 2418 2419 *len = (arr.count * sizeof(struct iwinfo_assoclist_entry)); 2420 return 0; 2421 } 2422 2423 return -1; 2424 } 2425 2426 static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg) 2427 { 2428 int *dbm_max = arg; 2429 int ch_cur, ch_cmp, bands_remain, freqs_remain; 2430 2431 struct nlattr **attr = nl80211_parse(msg); 2432 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1]; 2433 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1]; 2434 struct nlattr *band, *freq; 2435 2436 static const struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { 2437 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, 2438 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, 2439 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, 2440 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, 2441 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, 2442 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 }, 2443 }; 2444 2445 ch_cur = *dbm_max; /* value int* is initialized with channel by caller */ 2446 *dbm_max = -1; 2447 2448 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) 2449 { 2450 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band), 2451 nla_len(band), NULL); 2452 2453 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain) 2454 { 2455 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, 2456 nla_data(freq), nla_len(freq), freq_policy); 2457 2458 ch_cmp = nl80211_freq2channel(nla_get_u32( 2459 freqs[NL80211_FREQUENCY_ATTR_FREQ])); 2460 2461 if ((!ch_cur || (ch_cmp == ch_cur)) && 2462 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) 2463 { 2464 *dbm_max = (int)(0.01 * nla_get_u32( 2465 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])); 2466 2467 break; 2468 } 2469 } 2470 } 2471 2472 return NL_SKIP; 2473 } 2474 2475 static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len) 2476 { 2477 int err, ch_cur; 2478 int dbm_max = -1, dbm_cur, dbm_cnt; 2479 struct iwinfo_txpwrlist_entry entry; 2480 2481 if (nl80211_get_channel(ifname, &ch_cur)) 2482 ch_cur = 0; 2483 2484 /* initialize the value pointer with channel for callback */ 2485 dbm_max = ch_cur; 2486 2487 err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, 2488 nl80211_get_txpwrlist_cb, &dbm_max); 2489 2490 if (!err) 2491 { 2492 for (dbm_cur = 0, dbm_cnt = 0; 2493 dbm_cur < dbm_max; 2494 dbm_cur++, dbm_cnt++) 2495 { 2496 entry.dbm = dbm_cur; 2497 entry.mw = iwinfo_dbm2mw(dbm_cur); 2498 2499 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry)); 2500 } 2501 2502 entry.dbm = dbm_max; 2503 entry.mw = iwinfo_dbm2mw(dbm_max); 2504 2505 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry)); 2506 dbm_cnt++; 2507 2508 *len = dbm_cnt * sizeof(entry); 2509 return 0; 2510 } 2511 2512 return -1; 2513 } 2514 2515 static void nl80211_get_scancrypto(char *spec, struct iwinfo_crypto_entry *c) 2516 { 2517 int wpa_version = 0; 2518 char *p, *q, *proto, *suites; 2519 2520 c->enabled = 0; 2521 2522 for (p = strtok_r(spec, "[]", &q); p; p = strtok_r(NULL, "[]", &q)) { 2523 if (!strcmp(p, "WEP")) { 2524 c->enabled = 1; 2525 c->auth_suites = IWINFO_KMGMT_NONE; 2526 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; 2527 c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104; 2528 break; 2529 } 2530 2531 proto = strtok(p, "-"); 2532 suites = strtok(NULL, "]"); 2533 2534 if (!proto || !suites) 2535 continue; 2536 2537 if (!strcmp(proto, "WPA2") || !strcmp(proto, "RSN")) 2538 wpa_version = 2; 2539 else if (!strcmp(proto, "WPA")) 2540 wpa_version = 1; 2541 else 2542 continue; 2543 2544 c->enabled = 1; 2545 2546 parse_wpa_suites(suites, wpa_version, &c->wpa_version, &c->auth_suites); 2547 parse_wpa_ciphers(suites, &c->pair_ciphers); 2548 } 2549 } 2550 2551 2552 struct nl80211_scanlist { 2553 struct iwinfo_scanlist_entry *e; 2554 int len; 2555 }; 2556 2557 2558 static void nl80211_get_scanlist_ie(struct nlattr **bss, 2559 struct iwinfo_scanlist_entry *e) 2560 { 2561 int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 2562 unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 2563 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 }; 2564 int len; 2565 2566 while (ielen >= 2 && ielen >= ie[1]) 2567 { 2568 switch (ie[0]) 2569 { 2570 case 0: /* SSID */ 2571 case 114: /* Mesh ID */ 2572 if (e->ssid[0] == 0) { 2573 len = min(ie[1], IWINFO_ESSID_MAX_SIZE); 2574 memcpy(e->ssid, ie + 2, len); 2575 e->ssid[len] = 0; 2576 } 2577 break; 2578 2579 case 48: /* RSN */ 2580 iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1], 2581 IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x); 2582 break; 2583 2584 case 221: /* Vendor */ 2585 if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1) 2586 iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4, 2587 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK); 2588 break; 2589 case 61: /* HT operation */ 2590 if (ie[1] >= 3) { 2591 e->ht_chan_info.primary_chan = ie[2]; 2592 e->ht_chan_info.secondary_chan_off = ie[3] & 0x3; 2593 e->ht_chan_info.chan_width = (ie[4] & 0x4)>>2; 2594 } 2595 break; 2596 case 192: /* VHT operation */ 2597 if (ie[1] >= 3) { 2598 e->vht_chan_info.chan_width = ie[2]; 2599 e->vht_chan_info.center_chan_1 = ie[3]; 2600 e->vht_chan_info.center_chan_2 = ie[4]; 2601 } 2602 break; 2603 } 2604 2605 ielen -= ie[1] + 2; 2606 ie += ie[1] + 2; 2607 } 2608 } 2609 2610 static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg) 2611 { 2612 int8_t rssi; 2613 uint16_t caps; 2614 2615 struct nl80211_scanlist *sl = arg; 2616 struct nlattr **tb = nl80211_parse(msg); 2617 struct nlattr *bss[NL80211_BSS_MAX + 1]; 2618 2619 static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 2620 [NL80211_BSS_TSF] = { .type = NLA_U64 }, 2621 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, 2622 [NL80211_BSS_BSSID] = { 0 }, 2623 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, 2624 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, 2625 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 }, 2626 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, 2627 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, 2628 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 2629 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, 2630 [NL80211_BSS_BEACON_IES] = { 0 }, 2631 }; 2632 2633 if (!tb[NL80211_ATTR_BSS] || 2634 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], 2635 bss_policy) || 2636 !bss[NL80211_BSS_BSSID]) 2637 { 2638 return NL_SKIP; 2639 } 2640 2641 if (bss[NL80211_BSS_CAPABILITY]) 2642 caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); 2643 else 2644 caps = 0; 2645 2646 memset(sl->e, 0, sizeof(*sl->e)); 2647 memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6); 2648 2649 if (caps & (1<<1)) 2650 sl->e->mode = IWINFO_OPMODE_ADHOC; 2651 else if (caps & (1<<0)) 2652 sl->e->mode = IWINFO_OPMODE_MASTER; 2653 else 2654 sl->e->mode = IWINFO_OPMODE_MESHPOINT; 2655 2656 if (caps & (1<<4)) 2657 sl->e->crypto.enabled = 1; 2658 2659 if (bss[NL80211_BSS_FREQUENCY]) 2660 { 2661 sl->e->mhz = nla_get_u32(bss[NL80211_BSS_FREQUENCY]); 2662 sl->e->band = nl80211_freq2band(sl->e->mhz); 2663 sl->e->channel = nl80211_freq2channel(sl->e->mhz); 2664 } 2665 2666 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) 2667 nl80211_get_scanlist_ie(bss, sl->e); 2668 2669 if (bss[NL80211_BSS_SIGNAL_MBM]) 2670 { 2671 sl->e->signal = 2672 (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100); 2673 2674 rssi = sl->e->signal - 0x100; 2675 2676 if (rssi < -110) 2677 rssi = -110; 2678 else if (rssi > -40) 2679 rssi = -40; 2680 2681 sl->e->quality = (rssi + 110); 2682 sl->e->quality_max = 70; 2683 } 2684 2685 if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version) 2686 { 2687 sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; 2688 sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104; 2689 } 2690 2691 sl->e++; 2692 sl->len++; 2693 2694 return NL_SKIP; 2695 } 2696 2697 static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len) 2698 { 2699 struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf }; 2700 2701 if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL)) 2702 goto out; 2703 2704 if (nl80211_wait("nl80211", "scan", 2705 NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED)) 2706 goto out; 2707 2708 if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, 2709 nl80211_get_scanlist_cb, &sl)) 2710 goto out; 2711 2712 *len = sl.len * sizeof(struct iwinfo_scanlist_entry); 2713 return 0; 2714 2715 out: 2716 *len = 0; 2717 return -1; 2718 } 2719 2720 static int wpasupp_ssid_decode(const char *in, char *out, int outlen) 2721 { 2722 #define hex(x) \ 2723 (((x) >= 'a') ? ((x) - 'a' + 10) : \ 2724 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - ''))) 2725 2726 int len = 0; 2727 2728 while (*in) 2729 { 2730 if (len + 1 >= outlen) 2731 break; 2732 2733 switch (*in) 2734 { 2735 case '\\': 2736 in++; 2737 switch (*in) 2738 { 2739 case 'n': 2740 out[len++] = '\n'; in++; 2741 break; 2742 2743 case 'r': 2744 out[len++] = '\r'; in++; 2745 break; 2746 2747 case 't': 2748 out[len++] = '\t'; in++; 2749 break; 2750 2751 case 'e': 2752 out[len++] = '\033'; in++; 2753 break; 2754 2755 case 'x': 2756 if (isxdigit(*(in+1)) && isxdigit(*(in+2))) 2757 out[len++] = hex(*(in+1)) * 16 + hex(*(in+2)); 2758 in += 3; 2759 break; 2760 2761 default: 2762 out[len++] = *in++; 2763 break; 2764 } 2765 break; 2766 2767 default: 2768 out[len++] = *in++; 2769 break; 2770 } 2771 } 2772 2773 if (outlen > len) 2774 out[len] = '\0'; 2775 2776 return len; 2777 } 2778 2779 static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len) 2780 { 2781 int sock, qmax, rssi, tries, count = -1, ready = 0; 2782 char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096]; 2783 struct sockaddr_un local = { 0 }; 2784 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf; 2785 2786 sock = nl80211_wpactl_connect(ifname, &local); 2787 2788 if (sock < 0) 2789 return sock; 2790 2791 send(sock, "ATTACH", 6, 0); 2792 send(sock, "SCAN", 4, 0); 2793 2794 /* 2795 * wait for scan results: 2796 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan 2797 * 72 channels at most. We'll also receive two "OK" messages acknowledging 2798 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra 2799 * time to process the results, so try 72 + 2 + 1 times. 2800 */ 2801 for (tries = 0; tries < 75; tries++) 2802 { 2803 if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0) 2804 continue; 2805 2806 /* got an event notification */ 2807 if (reply[0] == '<') 2808 { 2809 /* scan results are ready */ 2810 if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS")) 2811 { 2812 /* send "SCAN_RESULTS" command */ 2813 ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12); 2814 break; 2815 } 2816 2817 /* is another unrelated event, retry */ 2818 tries--; 2819 } 2820 2821 /* scanning already in progress, keep awaiting results */ 2822 else if (!strcmp(reply, "FAIL-BUSY\n")) 2823 { 2824 tries--; 2825 } 2826 2827 /* another failure, abort */ 2828 else if (!strncmp(reply, "FAIL-", 5)) 2829 { 2830 break; 2831 } 2832 } 2833 2834 /* receive and parse scan results if the wait above didn't time out */ 2835 while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0) 2836 { 2837 /* received an event notification, receive again */ 2838 if (reply[0] == '<') 2839 continue; 2840 2841 nl80211_get_quality_max(ifname, &qmax); 2842 2843 for (line = strtok_r(reply, "\n", &pos); 2844 line != NULL; 2845 line = strtok_r(NULL, "\n", &pos)) 2846 { 2847 /* skip header line */ 2848 if (count < 0) 2849 { 2850 count++; 2851 continue; 2852 } 2853 2854 bssid = strtok(line, "\t"); 2855 freq = strtok(NULL, "\t"); 2856 signal = strtok(NULL, "\t"); 2857 flags = strtok(NULL, "\t"); 2858 ssid = strtok(NULL, "\n"); 2859 2860 if (!bssid || !freq || !signal || !flags) 2861 continue; 2862 2863 /* BSSID */ 2864 e->mac[0] = strtol(&bssid[0], NULL, 16); 2865 e->mac[1] = strtol(&bssid[3], NULL, 16); 2866 e->mac[2] = strtol(&bssid[6], NULL, 16); 2867 e->mac[3] = strtol(&bssid[9], NULL, 16); 2868 e->mac[4] = strtol(&bssid[12], NULL, 16); 2869 e->mac[5] = strtol(&bssid[15], NULL, 16); 2870 2871 /* SSID */ 2872 if (ssid) 2873 wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid)); 2874 else 2875 e->ssid[0] = 0; 2876 2877 /* Mode */ 2878 if (strstr(flags, "[MESH]")) 2879 e->mode = IWINFO_OPMODE_MESHPOINT; 2880 else if (strstr(flags, "[IBSS]")) 2881 e->mode = IWINFO_OPMODE_ADHOC; 2882 else 2883 e->mode = IWINFO_OPMODE_MASTER; 2884 2885 /* Channel */ 2886 e->mhz = atoi(freq); 2887 e->band = nl80211_freq2band(e->mhz); 2888 e->channel = nl80211_freq2channel(e->mhz); 2889 2890 /* Signal */ 2891 rssi = atoi(signal); 2892 e->signal = rssi; 2893 2894 /* Quality */ 2895 if (rssi < 0) 2896 { 2897 /* The cfg80211 wext compat layer assumes a signal range 2898 * of -110 dBm to -40 dBm, the quality value is derived 2899 * by adding 110 to the signal level */ 2900 if (rssi < -110) 2901 rssi = -110; 2902 else if (rssi > -40) 2903 rssi = -40; 2904 2905 e->quality = (rssi + 110); 2906 } 2907 else 2908 { 2909 e->quality = rssi; 2910 } 2911 2912 /* Max. Quality */ 2913 e->quality_max = qmax; 2914 2915 /* Crypto */ 2916 nl80211_get_scancrypto(flags, &e->crypto); 2917 2918 count++; 2919 e++; 2920 } 2921 2922 *len = count * sizeof(struct iwinfo_scanlist_entry); 2923 break; 2924 } 2925 2926 close(sock); 2927 unlink(local.sun_path); 2928 2929 return (count >= 0) ? 0 : -1; 2930 } 2931 2932 static int nl80211_get_scanlist(const char *ifname, char *buf, int *len) 2933 { 2934 char *res; 2935 int rv, mode; 2936 2937 *len = 0; 2938 2939 /* Got a radioX pseudo interface, find some interface on it or create one */ 2940 if (!strncmp(ifname, "radio", 5)) 2941 { 2942 /* Reuse existing interface */ 2943 if ((res = nl80211_phy2ifname(ifname)) != NULL) 2944 { 2945 return nl80211_get_scanlist(res, buf, len); 2946 } 2947 2948 /* Need to spawn a temporary iface for scanning */ 2949 else if ((res = nl80211_ifadd(ifname)) != NULL) 2950 { 2951 rv = nl80211_get_scanlist(res, buf, len); 2952 nl80211_ifdel(res); 2953 return rv; 2954 } 2955 } 2956 2957 /* WPA supplicant */ 2958 if (!nl80211_get_scanlist_wpactl(ifname, buf, len)) 2959 { 2960 return 0; 2961 } 2962 2963 /* station / ad-hoc / monitor scan */ 2964 else if (!nl80211_get_mode(ifname, &mode) && 2965 (mode == IWINFO_OPMODE_ADHOC || 2966 mode == IWINFO_OPMODE_MASTER || 2967 mode == IWINFO_OPMODE_CLIENT || 2968 mode == IWINFO_OPMODE_MONITOR) && 2969 iwinfo_ifup(ifname)) 2970 { 2971 return nl80211_get_scanlist_nl(ifname, buf, len); 2972 } 2973 2974 /* AP scan */ 2975 else 2976 { 2977 /* Got a temp interface, don't create yet another one */ 2978 if (!strncmp(ifname, "tmp.", 4)) 2979 { 2980 if (!iwinfo_ifup(ifname)) 2981 return -1; 2982 2983 rv = nl80211_get_scanlist_nl(ifname, buf, len); 2984 iwinfo_ifdown(ifname); 2985 return rv; 2986 } 2987 2988 /* Spawn a new scan interface */ 2989 else 2990 { 2991 if (!(res = nl80211_ifadd(ifname))) 2992 return -1; 2993 2994 iwinfo_ifmac(res); 2995 2996 /* if we can take the new interface up, the driver supports an 2997 * additional interface and there's no need to tear down the ap */ 2998 if (iwinfo_ifup(res)) 2999 { 3000 rv = nl80211_get_scanlist_nl(res, buf, len); 3001 iwinfo_ifdown(res); 3002 } 3003 3004 /* driver cannot create secondary interface, take down ap 3005 * during scan */ 3006 else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res)) 3007 { 3008 rv = nl80211_get_scanlist_nl(res, buf, len); 3009 iwinfo_ifdown(res); 3010 iwinfo_ifup(ifname); 3011 nl80211_hostapd_hup(ifname); 3012 } 3013 else 3014 rv = -1; 3015 3016 nl80211_ifdel(res); 3017 return rv; 3018 } 3019 } 3020 3021 return -1; 3022 } 3023 3024 static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg) 3025 { 3026 int bands_remain, freqs_remain; 3027 3028 struct nl80211_array_buf *arr = arg; 3029 struct iwinfo_freqlist_entry *e; 3030 3031 struct nlattr **attr = nl80211_parse(msg); 3032 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1]; 3033 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1]; 3034 struct nlattr *band, *freq; 3035 3036 e = arr->buf; 3037 e += arr->count; 3038 3039 if (attr[NL80211_ATTR_WIPHY_BANDS]) { 3040 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) 3041 { 3042 nla_parse(bands, NL80211_BAND_ATTR_MAX, 3043 nla_data(band), nla_len(band), NULL); 3044 3045 if (bands[NL80211_BAND_ATTR_FREQS]) { 3046 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain) 3047 { 3048 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, 3049 nla_data(freq), nla_len(freq), NULL); 3050 3051 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] || 3052 freqs[NL80211_FREQUENCY_ATTR_DISABLED]) 3053 continue; 3054 3055 e->band = nl80211_get_band(band->nla_type); 3056 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]); 3057 e->channel = nl80211_freq2channel(e->mhz); 3058 3059 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS]) 3060 e->flags |= IWINFO_FREQ_NO_HT40MINUS; 3061 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS]) 3062 e->flags |= IWINFO_FREQ_NO_HT40PLUS; 3063 if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ]) 3064 e->flags |= IWINFO_FREQ_NO_80MHZ; 3065 if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ]) 3066 e->flags |= IWINFO_FREQ_NO_160MHZ; 3067 if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ]) 3068 e->flags |= IWINFO_FREQ_NO_20MHZ; 3069 if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ]) 3070 e->flags |= IWINFO_FREQ_NO_10MHZ; 3071 if (freqs[NL80211_FREQUENCY_ATTR_NO_HE]) 3072 e->flags |= IWINFO_FREQ_NO_HE; 3073 if (freqs[NL80211_FREQUENCY_ATTR_NO_IR] && 3074 !freqs[NL80211_FREQUENCY_ATTR_RADAR]) 3075 e->flags |= IWINFO_FREQ_NO_IR; 3076 if (freqs[NL80211_FREQUENCY_ATTR_INDOOR_ONLY]) 3077 e->flags |= IWINFO_FREQ_INDOOR_ONLY; 3078 3079 /* keep backwards compatibility */ 3080 e->restricted = (e->flags & IWINFO_FREQ_NO_IR) ? 1 : 0; 3081 3082 e++; 3083 arr->count++; 3084 } 3085 } 3086 } 3087 } 3088 3089 return NL_SKIP; 3090 } 3091 3092 static int nl80211_get_freqlist(const char *ifname, char *buf, int *len) 3093 { 3094 struct nl80211_msg_conveyor *cv; 3095 struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; 3096 uint32_t features = nl80211_get_protocol_features(ifname); 3097 int flags; 3098 3099 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0; 3100 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags); 3101 if (!cv) 3102 goto out; 3103 3104 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 3105 if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr)) 3106 goto out; 3107 3108 *len = arr.count * sizeof(struct iwinfo_freqlist_entry); 3109 return 0; 3110 3111 nla_put_failure: 3112 nl80211_free(cv); 3113 out: 3114 *len = 0; 3115 return -1; 3116 } 3117 3118 static int nl80211_get_country_cb(struct nl_msg *msg, void *arg) 3119 { 3120 char *buf = arg; 3121 struct nlattr **attr = nl80211_parse(msg); 3122 3123 if (attr[NL80211_ATTR_REG_ALPHA2]) 3124 memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2); 3125 else 3126 buf[0] = 0; 3127 3128 return NL_SKIP; 3129 } 3130 3131 static int nl80211_get_country(const char *ifname, char *buf) 3132 { 3133 if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0, 3134 nl80211_get_country_cb, buf)) 3135 return -1; 3136 3137 return 0; 3138 } 3139 3140 static int nl80211_get_countrylist(const char *ifname, char *buf, int *len) 3141 { 3142 int count; 3143 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf; 3144 const struct iwinfo_iso3166_label *l; 3145 3146 for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++) 3147 { 3148 e->iso3166 = l->iso3166; 3149 e->ccode[0] = (l->iso3166 / 256); 3150 e->ccode[1] = (l->iso3166 % 256); 3151 e->ccode[2] = 0; 3152 } 3153 3154 *len = (count * sizeof(struct iwinfo_country_entry)); 3155 return 0; 3156 } 3157 3158 3159 struct nl80211_modes 3160 { 3161 bool ok; 3162 uint32_t hw; 3163 uint32_t ht; 3164 3165 uint8_t bands; 3166 3167 uint16_t nl_ht; 3168 uint32_t nl_vht; 3169 uint16_t he_phy_cap[6]; 3170 uint16_t eht_phy_cap[9]; 3171 }; 3172 3173 static void nl80211_eval_modelist(struct nl80211_modes *m) 3174 { 3175 /* Treat any nonzero capability as 11n */ 3176 if (m->nl_ht > 0) 3177 { 3178 m->hw |= IWINFO_80211_N; 3179 m->ht |= IWINFO_HTMODE_HT20; 3180 3181 if (m->nl_ht & (1 << 1)) 3182 m->ht |= IWINFO_HTMODE_HT40; 3183 } 3184 3185 if (m->he_phy_cap[0] != 0) { 3186 m->hw |= IWINFO_80211_AX; 3187 m->ht |= IWINFO_HTMODE_HE20; 3188 3189 if (m->he_phy_cap[0] & BIT(9)) 3190 m->ht |= IWINFO_HTMODE_HE40; 3191 if (m->he_phy_cap[0] & BIT(10)) 3192 m->ht |= IWINFO_HTMODE_HE40 | IWINFO_HTMODE_HE80; 3193 if (m->he_phy_cap[0] & BIT(11)) 3194 m->ht |= IWINFO_HTMODE_HE160; 3195 if (m->he_phy_cap[0] & BIT(12)) 3196 m->ht |= IWINFO_HTMODE_HE160 | IWINFO_HTMODE_HE80_80; 3197 } 3198 3199 if (m->eht_phy_cap[0] != 0) { 3200 m->hw |= IWINFO_80211_BE; 3201 m->ht |= IWINFO_HTMODE_EHT20; 3202 3203 if (m->he_phy_cap[0] & BIT(9)) 3204 m->ht |= IWINFO_HTMODE_EHT40; 3205 if (m->he_phy_cap[0] & BIT(10)) 3206 m->ht |= IWINFO_HTMODE_EHT40 | IWINFO_HTMODE_EHT80; 3207 if (m->he_phy_cap[0] & BIT(11)) 3208 m->ht |= IWINFO_HTMODE_EHT160; 3209 if (m->he_phy_cap[0] & BIT(12)) 3210 m->ht |= IWINFO_HTMODE_EHT160 | IWINFO_HTMODE_EHT80_80; 3211 if ((m->eht_phy_cap[0] & BIT(9)) && (m->bands & IWINFO_BAND_6)) 3212 m->ht |= IWINFO_HTMODE_EHT320; 3213 } 3214 3215 if (m->bands & IWINFO_BAND_24) 3216 { 3217 m->hw |= IWINFO_80211_B; 3218 m->hw |= IWINFO_80211_G; 3219 } 3220 3221 if (m->bands & IWINFO_BAND_5) 3222 { 3223 /* Treat any nonzero capability as 11ac */ 3224 if (m->nl_vht > 0) 3225 { 3226 m->hw |= IWINFO_80211_AC; 3227 m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80; 3228 3229 switch ((m->nl_vht >> 2) & 3) 3230 { 3231 case 2: 3232 m->ht |= IWINFO_HTMODE_VHT80_80; 3233 /* fall through */ 3234 3235 case 1: 3236 m->ht |= IWINFO_HTMODE_VHT160; 3237 } 3238 } 3239 else 3240 { 3241 m->hw |= IWINFO_80211_A; 3242 } 3243 } 3244 3245 if (m->bands & IWINFO_BAND_60) 3246 { 3247 m->hw |= IWINFO_80211_AD; 3248 } 3249 3250 } 3251 3252 static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg) 3253 { 3254 struct nl80211_modes *m = arg; 3255 int bands_remain; 3256 struct nlattr **attr = nl80211_parse(msg); 3257 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1]; 3258 struct nlattr *band; 3259 3260 if (attr[NL80211_ATTR_WIPHY_BANDS]) 3261 { 3262 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) 3263 { 3264 m->bands |= nl80211_get_band(band->nla_type); 3265 3266 nla_parse(bands, NL80211_BAND_ATTR_MAX, 3267 nla_data(band), nla_len(band), NULL); 3268 3269 if (bands[NL80211_BAND_ATTR_HT_CAPA]) 3270 m->nl_ht = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]); 3271 3272 if (bands[NL80211_BAND_ATTR_VHT_CAPA]) 3273 m->nl_vht = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]); 3274 3275 if (bands[NL80211_BAND_ATTR_IFTYPE_DATA]) { 3276 struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1]; 3277 struct nlattr *nl_iftype; 3278 int rem_band; 3279 int len; 3280 3281 nla_for_each_nested(nl_iftype, bands[NL80211_BAND_ATTR_IFTYPE_DATA], rem_band) { 3282 nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX, 3283 nla_data(nl_iftype), nla_len(nl_iftype), NULL); 3284 3285 // HE 3286 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) { 3287 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]); 3288 3289 if (len > sizeof(m->he_phy_cap) - 1) 3290 len = sizeof(m->he_phy_cap) - 1; 3291 memcpy(&((__u8 *)m->he_phy_cap)[1], 3292 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]), 3293 len); 3294 } 3295 3296 // EHT 3297 if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]) { 3298 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]); 3299 3300 if (len > sizeof(m->eht_phy_cap) - 1) 3301 len = sizeof(m->eht_phy_cap) - 1; 3302 memcpy(&((uint8_t *)m->eht_phy_cap)[1], 3303 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]), 3304 len); 3305 } 3306 } 3307 } 3308 } 3309 3310 m->ok = 1; 3311 } 3312 3313 return NL_SKIP; 3314 } 3315 3316 static int nl80211_get_hwmodelist(const char *ifname, int *buf) 3317 { 3318 struct nl80211_msg_conveyor *cv; 3319 struct nl80211_modes m = {}; 3320 uint32_t features = nl80211_get_protocol_features(ifname); 3321 int flags; 3322 3323 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0; 3324 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags); 3325 if (!cv) 3326 goto out; 3327 3328 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 3329 if (nl80211_send(cv, nl80211_get_modelist_cb, &m)) 3330 goto nla_put_failure; 3331 3332 nl80211_eval_modelist(&m); 3333 3334 *buf = m.hw; 3335 3336 return 0; 3337 3338 nla_put_failure: 3339 nl80211_free(cv); 3340 out: 3341 return -1; 3342 } 3343 3344 struct chan_info { 3345 int width; 3346 int mode; 3347 }; 3348 3349 static int nl80211_get_htmode_cb(struct nl_msg *msg, void *arg) 3350 { 3351 struct nlattr **tb = nl80211_parse(msg); 3352 struct nlattr *cur; 3353 struct chan_info *chn = arg; 3354 3355 if ((cur = tb[NL80211_ATTR_CHANNEL_WIDTH])) 3356 chn->width = nla_get_u32(cur); 3357 3358 if ((cur = tb[NL80211_ATTR_BSS_HT_OPMODE])) 3359 chn->mode = nla_get_u32(cur); 3360 3361 return NL_SKIP; 3362 } 3363 3364 static int nl80211_get_htmode(const char *ifname, int *buf) 3365 { 3366 struct chan_info chn = { 0 }; 3367 char *res, b[2] = { 0 }; 3368 int err; 3369 bool he = false; 3370 bool eht = false; 3371 3372 res = nl80211_phy2ifname(ifname); 3373 *buf = 0; 3374 3375 err = nl80211_request(res ? res : ifname, 3376 NL80211_CMD_GET_INTERFACE, 0, 3377 nl80211_get_htmode_cb, &chn); 3378 if (err) 3379 return -1; 3380 3381 if (nl80211_hostapd_query(res ? res : ifname, "ieee80211be", b, sizeof(b))) 3382 eht = b[0] == '1'; 3383 3384 if (nl80211_hostapd_query(res ? res : ifname, "ieee80211ax", b, sizeof(b))) 3385 he = b[0] == '1'; 3386 else if (nl80211_wpactl_query(res ? res : ifname, "wifi_generation", b, sizeof(b))) { 3387 he = b[0] == '6'; 3388 eht = b[0] == '7'; 3389 } 3390 3391 switch (chn.width) { 3392 case NL80211_CHAN_WIDTH_20: 3393 if (eht) 3394 *buf = IWINFO_HTMODE_EHT20; 3395 else if (he) 3396 *buf = IWINFO_HTMODE_HE20; 3397 else if (chn.mode == -1) 3398 *buf = IWINFO_HTMODE_VHT20; 3399 else 3400 *buf = IWINFO_HTMODE_HT20; 3401 break; 3402 case NL80211_CHAN_WIDTH_40: 3403 if (eht) 3404 *buf = IWINFO_HTMODE_EHT40; 3405 else if (he) 3406 *buf = IWINFO_HTMODE_HE40; 3407 else if (chn.mode == -1) 3408 *buf = IWINFO_HTMODE_VHT40; 3409 else 3410 *buf = IWINFO_HTMODE_HT40; 3411 break; 3412 case NL80211_CHAN_WIDTH_80: 3413 if (eht) 3414 *buf = IWINFO_HTMODE_EHT80; 3415 else if (he) 3416 *buf = IWINFO_HTMODE_HE80; 3417 else 3418 *buf = IWINFO_HTMODE_VHT80; 3419 break; 3420 case NL80211_CHAN_WIDTH_80P80: 3421 if (eht) 3422 *buf = IWINFO_HTMODE_EHT80_80; 3423 else if (he) 3424 *buf = IWINFO_HTMODE_HE80_80; 3425 else 3426 *buf = IWINFO_HTMODE_VHT80_80; 3427 break; 3428 case NL80211_CHAN_WIDTH_160: 3429 if (eht) 3430 *buf = IWINFO_HTMODE_EHT160; 3431 else if (he) 3432 *buf = IWINFO_HTMODE_HE160; 3433 else 3434 *buf = IWINFO_HTMODE_VHT160; 3435 break; 3436 case NL80211_CHAN_WIDTH_320: 3437 *buf = IWINFO_HTMODE_EHT320; 3438 break; 3439 case NL80211_CHAN_WIDTH_5: 3440 case NL80211_CHAN_WIDTH_10: 3441 case NL80211_CHAN_WIDTH_20_NOHT: 3442 *buf = IWINFO_HTMODE_NOHT; 3443 break; 3444 default: 3445 return -1; 3446 } 3447 3448 return 0; 3449 } 3450 3451 static int nl80211_get_htmodelist(const char *ifname, int *buf) 3452 { 3453 struct nl80211_msg_conveyor *cv; 3454 struct nl80211_modes m = {}; 3455 uint32_t features = nl80211_get_protocol_features(ifname); 3456 int flags; 3457 3458 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0; 3459 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags); 3460 if (!cv) 3461 goto out; 3462 3463 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 3464 if (nl80211_send(cv, nl80211_get_modelist_cb, &m)) 3465 goto nla_put_failure; 3466 3467 nl80211_eval_modelist(&m); 3468 3469 *buf = m.ht; 3470 3471 return 0; 3472 3473 nla_put_failure: 3474 nl80211_free(cv); 3475 out: 3476 return -1; 3477 } 3478 3479 3480 static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg) 3481 { 3482 struct nlattr **attr = nl80211_parse(msg); 3483 struct nlattr *comb; 3484 int *ret = arg; 3485 int comb_rem, limit_rem, mode_rem; 3486 3487 *ret = 0; 3488 if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS]) 3489 return NL_SKIP; 3490 3491 nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem) 3492 { 3493 static const struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = { 3494 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED }, 3495 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 }, 3496 }; 3497 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1]; 3498 static const struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = { 3499 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED }, 3500 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 }, 3501 }; 3502 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1]; 3503 struct nlattr *limit; 3504 3505 nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy); 3506 3507 if (!tb_comb[NL80211_IFACE_COMB_LIMITS]) 3508 continue; 3509 3510 nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem) 3511 { 3512 struct nlattr *mode; 3513 3514 nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy); 3515 3516 if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] || 3517 !tb_limit[NL80211_IFACE_LIMIT_MAX]) 3518 continue; 3519 3520 if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2) 3521 continue; 3522 3523 nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) { 3524 if (nla_type(mode) == NL80211_IFTYPE_AP) 3525 *ret = 1; 3526 } 3527 } 3528 } 3529 3530 return NL_SKIP; 3531 } 3532 3533 static int nl80211_get_mbssid_support(const char *ifname, int *buf) 3534 { 3535 if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, 3536 nl80211_get_ifcomb_cb, buf)) 3537 return -1; 3538 3539 return 0; 3540 } 3541 3542 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname) 3543 { 3544 char *phy, path[PATH_MAX]; 3545 3546 /* Try to determine the phy name from the given interface */ 3547 phy = nl80211_ifname2phy(ifname); 3548 3549 snprintf(path, sizeof(path), "/sys/class/%s/%s/device/of_node/compatible", 3550 phy ? "ieee80211" : "net", phy ? phy : ifname); 3551 3552 if (nl80211_readstr(path, id->compatible, sizeof(id->compatible)) <= 0) 3553 return -1; 3554 3555 return 0; 3556 } 3557 3558 3559 static int nl80211_get_hardware_id(const char *ifname, char *buf) 3560 { 3561 struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf; 3562 char *phy, num[8], path[PATH_MAX]; 3563 int i; 3564 3565 struct { const char *path; uint16_t *dest; } lookup[] = { 3566 { "vendor", &id->vendor_id }, 3567 { "device", &id->device_id }, 3568 { "subsystem_vendor", &id->subsystem_vendor_id }, 3569 { "subsystem_device", &id->subsystem_device_id }, 3570 { "../idVendor", &id->subsystem_vendor_id }, 3571 { "../idProduct", &id->subsystem_device_id } 3572 }; 3573 3574 memset(id, 0, sizeof(*id)); 3575 3576 /* Try to determine the phy name from the given interface */ 3577 phy = nl80211_ifname2phy(ifname); 3578 3579 for (i = 0; i < ARRAY_SIZE(lookup); i++) 3580 { 3581 snprintf(path, sizeof(path), "/sys/class/%s/%s/device/%s", 3582 phy ? "ieee80211" : "net", 3583 phy ? phy : ifname, lookup[i].path); 3584 3585 if (nl80211_readstr(path, num, sizeof(num)) > 0) 3586 *lookup[i].dest = strtoul(num, NULL, 16); 3587 } 3588 3589 /* Failed to obtain hardware PCI/USB IDs... */ 3590 if (id->vendor_id == 0 && id->device_id == 0 && 3591 id->subsystem_vendor_id == 0 && id->subsystem_device_id == 0) 3592 /* ... first fallback to FDT ... */ 3593 if (nl80211_hardware_id_from_fdt(id, ifname) == -1) 3594 /* ... then board config */ 3595 return iwinfo_hardware_id_from_mtd(id); 3596 3597 return 0; 3598 } 3599 3600 static const struct iwinfo_hardware_entry * 3601 nl80211_get_hardware_entry(const char *ifname) 3602 { 3603 struct iwinfo_hardware_id id; 3604 3605 if (nl80211_get_hardware_id(ifname, (char *)&id)) 3606 return NULL; 3607 3608 return iwinfo_hardware(&id); 3609 } 3610 3611 static int nl80211_get_hardware_name(const char *ifname, char *buf) 3612 { 3613 const struct iwinfo_hardware_entry *hw; 3614 3615 if (!(hw = nl80211_get_hardware_entry(ifname))) 3616 sprintf(buf, "Generic MAC80211"); 3617 else 3618 sprintf(buf, "%s %s", hw->vendor_name, hw->device_name); 3619 3620 return 0; 3621 } 3622 3623 static int nl80211_get_txpower_offset(const char *ifname, int *buf) 3624 { 3625 const struct iwinfo_hardware_entry *hw; 3626 3627 if (!(hw = nl80211_get_hardware_entry(ifname))) 3628 return -1; 3629 3630 *buf = hw->txpower_offset; 3631 return 0; 3632 } 3633 3634 static int nl80211_get_frequency_offset(const char *ifname, int *buf) 3635 { 3636 const struct iwinfo_hardware_entry *hw; 3637 3638 if (!(hw = nl80211_get_hardware_entry(ifname))) 3639 return -1; 3640 3641 *buf = hw->frequency_offset; 3642 return 0; 3643 } 3644 3645 static int nl80211_lookup_phyname(const char *section, char *buf) 3646 { 3647 const char *name; 3648 int idx; 3649 3650 if (!strncmp(section, "path=", 5)) 3651 idx = nl80211_phy_idx_from_path(section + 5); 3652 else if (!strncmp(section, "macaddr=", 8)) 3653 idx = nl80211_phy_idx_from_macaddr(section + 8); 3654 else 3655 idx = nl80211_phy_idx_from_uci(section); 3656 3657 if (idx < 0) 3658 return -1; 3659 3660 name = nl80211_phyidx2name(idx); 3661 if (!name) 3662 return -1; 3663 3664 strcpy(buf, name); 3665 return 0; 3666 } 3667 3668 static int nl80211_phy_path(const char *phyname, const char **path) 3669 { 3670 if (strchr(phyname, '/')) 3671 return -1; 3672 3673 *path = nl80211_phy_path_str(phyname); 3674 if (!*path) 3675 return -1; 3676 3677 return 0; 3678 } 3679 3680 const struct iwinfo_ops nl80211_ops = { 3681 .name = "nl80211", 3682 .probe = nl80211_probe, 3683 .channel = nl80211_get_channel, 3684 .center_chan1 = nl80211_get_center_chan1, 3685 .center_chan2 = nl80211_get_center_chan2, 3686 .frequency = nl80211_get_frequency, 3687 .frequency_offset = nl80211_get_frequency_offset, 3688 .txpower = nl80211_get_txpower, 3689 .txpower_offset = nl80211_get_txpower_offset, 3690 .bitrate = nl80211_get_bitrate, 3691 .signal = nl80211_get_signal, 3692 .noise = nl80211_get_noise, 3693 .quality = nl80211_get_quality, 3694 .quality_max = nl80211_get_quality_max, 3695 .mbssid_support = nl80211_get_mbssid_support, 3696 .hwmodelist = nl80211_get_hwmodelist, 3697 .htmodelist = nl80211_get_htmodelist, 3698 .htmode = nl80211_get_htmode, 3699 .mode = nl80211_get_mode, 3700 .ssid = nl80211_get_ssid, 3701 .bssid = nl80211_get_bssid, 3702 .country = nl80211_get_country, 3703 .hardware_id = nl80211_get_hardware_id, 3704 .hardware_name = nl80211_get_hardware_name, 3705 .encryption = nl80211_get_encryption, 3706 .phyname = nl80211_get_phyname, 3707 .assoclist = nl80211_get_assoclist, 3708 .txpwrlist = nl80211_get_txpwrlist, 3709 .scanlist = nl80211_get_scanlist, 3710 .freqlist = nl80211_get_freqlist, 3711 .countrylist = nl80211_get_countrylist, 3712 .survey = nl80211_get_survey, 3713 .lookup_phy = nl80211_lookup_phyname, 3714 .phy_path = nl80211_phy_path, 3715 .close = nl80211_close 3716 }; 3717
This page was automatically generated by LXR 0.3.1. • OpenWrt