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(const int channel, const char *band, const int opclass) 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 // 6GHz: opclass between 131 and 137 728 else if (opclass >= 131 && opclass <= 137) 729 { 730 if (channel == 2) 731 return 5935; 732 if (channel < 233) 733 return (channel * 5) + 5950; 734 } 735 else 736 { 737 if (channel >= 182 && channel <= 196) 738 return (channel * 5) + 4000; 739 else 740 return (channel * 5) + 5000; 741 } 742 743 return 0; 744 } 745 746 static uint8_t nl80211_freq2band(int freq) 747 { 748 if (freq >= 2412 && freq <= 2484) 749 return IWINFO_BAND_24; 750 else if (freq >= 5160 && freq <= 5885) 751 return IWINFO_BAND_5; 752 else if (freq >= 5925 && freq <= 7125) 753 return IWINFO_BAND_6; 754 else if (freq >= 58320 && freq <= 69120) 755 return IWINFO_BAND_60; 756 757 return 0; 758 } 759 760 static int nl80211_phyname_cb(struct nl_msg *msg, void *arg) 761 { 762 char *buf = arg; 763 struct nlattr **attr = nl80211_parse(msg); 764 765 if (attr[NL80211_ATTR_WIPHY_NAME]) 766 memcpy(buf, nla_data(attr[NL80211_ATTR_WIPHY_NAME]), 767 nla_len(attr[NL80211_ATTR_WIPHY_NAME])); 768 else 769 buf[0] = 0; 770 771 return NL_SKIP; 772 } 773 774 static char * nl80211_ifname2phy(const char *ifname) 775 { 776 static char phy[32] = { 0 }; 777 778 memset(phy, 0, sizeof(phy)); 779 780 nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, 781 nl80211_phyname_cb, phy); 782 783 return phy[0] ? phy : NULL; 784 } 785 786 static char * nl80211_phyidx2name(unsigned int idx) 787 { 788 struct nl80211_msg_conveyor *cv; 789 static char phy[32] = { 0 }; 790 791 if (nl80211_init() < 0) 792 return NULL; 793 794 cv = nl80211_new(nls->nl80211, NL80211_CMD_GET_WIPHY, 0); 795 if (!cv) 796 return NULL; 797 798 NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, idx); 799 800 memset(phy, 0, sizeof(phy)); 801 nl80211_send(cv, nl80211_phyname_cb, phy); 802 803 return phy[0] ? phy : NULL; 804 805 nla_put_failure: 806 return NULL; 807 } 808 809 static char * nl80211_phy2ifname(const char *ifname) 810 { 811 int ifidx = -1, cifidx, lmode = 1, clmode, phyidx; 812 char buffer[512]; 813 static char nif[IFNAMSIZ] = { 0 }; 814 DIR *d; 815 struct dirent *e; 816 817 /* Only accept phy name in the form of phy%d or radio%d */ 818 if (!ifname) 819 return NULL; 820 821 phyidx = nl80211_phy_idx_from_phy(ifname); 822 if (phyidx < 0) 823 phyidx = nl80211_phy_idx_from_uci(ifname);; 824 if (phyidx < 0) 825 return NULL; 826 827 memset(nif, 0, sizeof(nif)); 828 829 if ((d = opendir("/sys/class/net")) != NULL) 830 { 831 while ((e = readdir(d)) != NULL) 832 { 833 snprintf(buffer, sizeof(buffer), 834 "/sys/class/net/%s/phy80211/index", e->d_name); 835 if (nl80211_readint(buffer) != phyidx) 836 continue; 837 838 snprintf(buffer, sizeof(buffer), 839 "/sys/class/net/%s/ifindex", e->d_name); 840 cifidx = nl80211_readint(buffer); 841 842 if (cifidx < 0) 843 continue; 844 845 snprintf(buffer, sizeof(buffer), 846 "/sys/class/net/%s/link_mode", e->d_name); 847 clmode = nl80211_readint(buffer); 848 849 /* prefer non-supplicant-based devices */ 850 if ((ifidx < 0) || (cifidx < ifidx) || ((lmode == 1) && (clmode != 1))) 851 { 852 ifidx = cifidx; 853 lmode = clmode; 854 strncpy(nif, e->d_name, sizeof(nif) - 1); 855 } 856 } 857 858 closedir(d); 859 } 860 861 return nif[0] ? nif : NULL; 862 } 863 864 static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg) 865 { 866 int *mode = arg; 867 struct nlattr **tb = nl80211_parse(msg); 868 const int ifmodes[NL80211_IFTYPE_MAX + 1] = { 869 IWINFO_OPMODE_UNKNOWN, /* unspecified */ 870 IWINFO_OPMODE_ADHOC, /* IBSS */ 871 IWINFO_OPMODE_CLIENT, /* managed */ 872 IWINFO_OPMODE_MASTER, /* AP */ 873 IWINFO_OPMODE_AP_VLAN, /* AP/VLAN */ 874 IWINFO_OPMODE_WDS, /* WDS */ 875 IWINFO_OPMODE_MONITOR, /* monitor */ 876 IWINFO_OPMODE_MESHPOINT, /* mesh point */ 877 IWINFO_OPMODE_P2P_CLIENT, /* P2P-client */ 878 IWINFO_OPMODE_P2P_GO, /* P2P-GO */ 879 }; 880 881 if (tb[NL80211_ATTR_IFTYPE]) 882 *mode = ifmodes[nla_get_u32(tb[NL80211_ATTR_IFTYPE])]; 883 884 return NL_SKIP; 885 } 886 887 888 static int nl80211_get_mode(const char *ifname, int *buf) 889 { 890 char *res; 891 892 *buf = IWINFO_OPMODE_UNKNOWN; 893 894 res = nl80211_phy2ifname(ifname); 895 896 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 897 nl80211_get_mode_cb, buf); 898 899 return (*buf == IWINFO_OPMODE_UNKNOWN) ? -1 : 0; 900 } 901 902 static int __nl80211_hostapd_query(const char *ifname, ...) 903 { 904 va_list ap, ap_cur; 905 char *phy, *search, *dest, *key, *val, buf[128]; 906 int len, mode, found = 0, match = 1; 907 FILE *fp; 908 909 if (nl80211_get_mode(ifname, &mode)) 910 return 0; 911 912 if (mode != IWINFO_OPMODE_MASTER && mode != IWINFO_OPMODE_AP_VLAN) 913 return 0; 914 915 phy = nl80211_ifname2phy(ifname); 916 917 if (!phy) 918 return 0; 919 920 snprintf(buf, sizeof(buf), "/var/run/hostapd-%s.conf", phy); 921 fp = fopen(buf, "r"); 922 923 if (!fp) 924 return 0; 925 926 va_start(ap, ifname); 927 928 /* clear all destination buffers */ 929 va_copy(ap_cur, ap); 930 931 while ((search = va_arg(ap_cur, char *)) != NULL) 932 { 933 dest = va_arg(ap_cur, char *); 934 len = va_arg(ap_cur, int); 935 936 memset(dest, 0, len); 937 } 938 939 va_end(ap_cur); 940 941 /* iterate applicable lines and copy found values into dest buffers */ 942 while (fgets(buf, sizeof(buf), fp)) 943 { 944 key = strtok(buf, " =\t\n"); 945 val = strtok(NULL, "\n"); 946 947 if (!key || !val || !*key || *key == '#') 948 continue; 949 950 if (!strcmp(key, "interface") || !strcmp(key, "bss")) 951 match = !strcmp(ifname, val); 952 953 if (!match) 954 continue; 955 956 va_copy(ap_cur, ap); 957 958 while ((search = va_arg(ap_cur, char *)) != NULL) 959 { 960 dest = va_arg(ap_cur, char *); 961 len = va_arg(ap_cur, int); 962 963 if (!strcmp(search, key)) 964 { 965 strncpy(dest, val, len - 1); 966 found++; 967 break; 968 } 969 } 970 971 va_end(ap_cur); 972 } 973 974 fclose(fp); 975 976 va_end(ap); 977 978 return found; 979 } 980 981 #define nl80211_hostapd_query(ifname, ...) \ 982 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL) 983 984 985 static inline int nl80211_wpactl_recv(int sock, char *buf, int blen) 986 { 987 fd_set rfds; 988 struct timeval tv = { 0, 256000 }; 989 990 FD_ZERO(&rfds); 991 FD_SET(sock, &rfds); 992 993 memset(buf, 0, blen); 994 995 if (select(sock + 1, &rfds, NULL, NULL, &tv) < 0) 996 return -1; 997 998 if (!FD_ISSET(sock, &rfds)) 999 return -1; 1000 1001 return recv(sock, buf, blen - 1, 0); 1002 } 1003 1004 static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local) 1005 { 1006 struct sockaddr_un remote = { 0 }; 1007 size_t remote_length, local_length; 1008 1009 int sock = socket(PF_UNIX, SOCK_DGRAM, 0); 1010 if (sock < 0) 1011 return sock; 1012 1013 remote.sun_family = AF_UNIX; 1014 remote_length = sizeof(remote.sun_family) + 1015 sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname); 1016 1017 /* Set client socket file permissions so that bind() creates the client 1018 * socket with these permissions and there is no need to try to change 1019 * them with chmod() after bind() which would have potential issues with 1020 * race conditions. These permissions are needed to make sure the server 1021 * side (wpa_supplicant or hostapd) can reply to the control interface 1022 * messages. 1023 * 1024 * The lchown() calls below after bind() are also part of the needed 1025 * operations to allow the response to go through. Those are using the 1026 * no-deference-symlinks version to avoid races. */ 1027 fchmod(sock, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 1028 1029 if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0) 1030 { 1031 close(sock); 1032 return -1; 1033 } 1034 1035 if (connect(sock, (struct sockaddr *)&remote, remote_length)) 1036 { 1037 close(sock); 1038 return -1; 1039 } 1040 1041 local->sun_family = AF_UNIX; 1042 local_length = sizeof(local->sun_family) + 1043 sprintf(local->sun_path, "/var/run/iwinfo-%s-%d", ifname, getpid()); 1044 1045 if (bind(sock, (struct sockaddr *)local, local_length) < 0) 1046 { 1047 close(sock); 1048 return -1; 1049 } 1050 1051 /* Set group even if we do not have privileges to change owner */ 1052 lchown(local->sun_path, -1, 101); 1053 lchown(local->sun_path, 101, 101); 1054 1055 return sock; 1056 } 1057 1058 static int __nl80211_wpactl_query(const char *ifname, ...) 1059 { 1060 va_list ap, ap_cur; 1061 struct sockaddr_un local = { 0 }; 1062 int len, mode, found = 0, sock = -1; 1063 char *search, *dest, *key, *val, *line, *pos, buf[512]; 1064 1065 if (nl80211_get_mode(ifname, &mode)) 1066 return 0; 1067 1068 if (mode != IWINFO_OPMODE_CLIENT && 1069 mode != IWINFO_OPMODE_ADHOC && 1070 mode != IWINFO_OPMODE_MESHPOINT) 1071 return 0; 1072 1073 sock = nl80211_wpactl_connect(ifname, &local); 1074 1075 if (sock < 0) 1076 return 0; 1077 1078 va_start(ap, ifname); 1079 1080 /* clear all destination buffers */ 1081 va_copy(ap_cur, ap); 1082 1083 while ((search = va_arg(ap_cur, char *)) != NULL) 1084 { 1085 dest = va_arg(ap_cur, char *); 1086 len = va_arg(ap_cur, int); 1087 1088 memset(dest, 0, len); 1089 } 1090 1091 va_end(ap_cur); 1092 1093 send(sock, "STATUS", 6, 0); 1094 1095 while (true) 1096 { 1097 if (nl80211_wpactl_recv(sock, buf, sizeof(buf)) <= 0) 1098 break; 1099 1100 if (buf[0] == '<') 1101 continue; 1102 1103 for (line = strtok_r(buf, "\n", &pos); 1104 line != NULL; 1105 line = strtok_r(NULL, "\n", &pos)) 1106 { 1107 key = strtok(line, "="); 1108 val = strtok(NULL, "\n"); 1109 1110 if (!key || !val) 1111 continue; 1112 1113 va_copy(ap_cur, ap); 1114 1115 while ((search = va_arg(ap_cur, char *)) != NULL) 1116 { 1117 dest = va_arg(ap_cur, char *); 1118 len = va_arg(ap_cur, int); 1119 1120 if (!strcmp(search, key)) 1121 { 1122 strncpy(dest, val, len - 1); 1123 found++; 1124 break; 1125 } 1126 } 1127 1128 va_end(ap_cur); 1129 } 1130 1131 break; 1132 } 1133 1134 va_end(ap); 1135 1136 close(sock); 1137 unlink(local.sun_path); 1138 1139 return found; 1140 } 1141 1142 #define nl80211_wpactl_query(ifname, ...) \ 1143 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL) 1144 1145 1146 static char * nl80211_ifadd(const char *ifname) 1147 { 1148 char path[PATH_MAX]; 1149 static char nif[IFNAMSIZ] = { 0 }; 1150 struct nl80211_msg_conveyor *req; 1151 FILE *sysfs; 1152 1153 req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0); 1154 if (req) 1155 { 1156 snprintf(nif, sizeof(nif), "tmp.%s", ifname); 1157 1158 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif); 1159 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION); 1160 1161 nl80211_send(req, NULL, NULL); 1162 1163 snprintf(path, sizeof(path) - 1, 1164 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif); 1165 1166 if ((sysfs = fopen(path, "w")) != NULL) 1167 { 1168 fwrite("\n", 1, 2, sysfs); 1169 fclose(sysfs); 1170 } 1171 1172 return nif; 1173 1174 nla_put_failure: 1175 nl80211_free(req); 1176 } 1177 1178 return NULL; 1179 } 1180 1181 static void nl80211_ifdel(const char *ifname) 1182 { 1183 struct nl80211_msg_conveyor *req; 1184 1185 req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0); 1186 if (req) 1187 { 1188 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname); 1189 1190 nl80211_send(req, NULL, NULL); 1191 return; 1192 1193 nla_put_failure: 1194 nl80211_free(req); 1195 } 1196 } 1197 1198 static void nl80211_hostapd_hup(const char *ifname) 1199 { 1200 int fd, pid = 0; 1201 char buf[32]; 1202 char *phy = nl80211_ifname2phy(ifname); 1203 1204 if (phy) 1205 { 1206 snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy); 1207 if ((fd = open(buf, O_RDONLY)) >= 0) 1208 { 1209 if (read(fd, buf, sizeof(buf)) > 0) 1210 pid = atoi(buf); 1211 1212 close(fd); 1213 } 1214 1215 if (pid > 0) 1216 kill(pid, 1); 1217 } 1218 } 1219 1220 1221 static int nl80211_probe(const char *ifname) 1222 { 1223 return !!nl80211_ifname2phy(ifname); 1224 } 1225 1226 struct nl80211_ssid_bssid { 1227 unsigned char *ssid; 1228 unsigned char bssid[7]; 1229 }; 1230 1231 static int nl80211_get_macaddr_cb(struct nl_msg *msg, void *arg) 1232 { 1233 struct nl80211_ssid_bssid *sb = arg; 1234 struct nlattr **tb = nl80211_parse(msg); 1235 1236 if (tb[NL80211_ATTR_MAC]) { 1237 sb->bssid[0] = 1; 1238 memcpy(sb->bssid + 1, nla_data(tb[NL80211_ATTR_MAC]), 1239 sizeof(sb->bssid) - 1); 1240 } 1241 1242 return NL_SKIP; 1243 } 1244 1245 static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg) 1246 { 1247 int ielen; 1248 unsigned char *ie; 1249 struct nl80211_ssid_bssid *sb = arg; 1250 struct nlattr **tb = nl80211_parse(msg); 1251 struct nlattr *bss[NL80211_BSS_MAX + 1]; 1252 1253 static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 1254 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 }, 1255 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 1256 }; 1257 1258 if (!tb[NL80211_ATTR_BSS] || 1259 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], 1260 bss_policy) || 1261 !bss[NL80211_BSS_BSSID] || 1262 !bss[NL80211_BSS_STATUS] || 1263 !bss[NL80211_BSS_INFORMATION_ELEMENTS]) 1264 { 1265 return NL_SKIP; 1266 } 1267 1268 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) 1269 { 1270 case NL80211_BSS_STATUS_ASSOCIATED: 1271 case NL80211_BSS_STATUS_AUTHENTICATED: 1272 case NL80211_BSS_STATUS_IBSS_JOINED: 1273 1274 if (sb->ssid) 1275 { 1276 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 1277 ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 1278 1279 while (ielen >= 2 && ielen >= ie[1]) 1280 { 1281 if (ie[0] == 0) 1282 { 1283 memcpy(sb->ssid, ie + 2, min(ie[1], IWINFO_ESSID_MAX_SIZE)); 1284 return NL_SKIP; 1285 } 1286 1287 ielen -= ie[1] + 2; 1288 ie += ie[1] + 2; 1289 } 1290 } 1291 else 1292 { 1293 sb->bssid[0] = 1; 1294 memcpy(sb->bssid + 1, nla_data(bss[NL80211_BSS_BSSID]), 6); 1295 return NL_SKIP; 1296 } 1297 1298 default: 1299 return NL_SKIP; 1300 } 1301 } 1302 1303 /* 1304 * This matches the behaviour of hostapd's wpa_config_parse_string 1305 * 1306 * (double quoted string, hexdump, printf-escaped string) 1307 * ssid2="test" 1308 * ssid2=74657374 1309 * ssid2=P"hello\nthere" 1310 */ 1311 static int parse_ssid2(const char *value, char *buf, size_t buflen) 1312 { 1313 const char *pos; 1314 size_t len, i; 1315 1316 if (*value == '"') { 1317 value++; 1318 pos = strrchr(value, '"'); 1319 if (!pos || pos[1] != '\0') 1320 return -1; 1321 len = pos - value; 1322 if (len >= buflen) 1323 len = buflen - 1; 1324 memcpy(buf, value, len); 1325 buf[len] = '\0'; 1326 return 0; 1327 } else if (*value == 'P' && value[1] == '"') { 1328 value += 2; 1329 pos = strrchr(value, '"'); 1330 if (!pos || pos[1] != '\0') 1331 return -1; 1332 len = 0; 1333 while (*value && value < pos && len < buflen - 1) { 1334 if (*value == '\\' && value + 1 < pos) { 1335 value++; 1336 if (*value == 'x' && value + 2 < pos) { 1337 char hex[3] = {value[1], value[2], 0}; 1338 buf[len++] = strtol(hex, NULL, 16); 1339 value += 3; 1340 } else if (*value >= '' && *value <= '7' && value + 2 < pos && 1341 value[1] >= '' && value[1] <= '7' && 1342 value[2] >= '' && value[2] <= '7') { 1343 buf[len++] = ((value[0] - '') << 6) | 1344 ((value[1] - '') << 3) | 1345 (value[2] - ''); 1346 value += 3; 1347 } else { 1348 switch (*value) { 1349 case 'n': buf[len++] = '\n'; break; 1350 case 'r': buf[len++] = '\r'; break; 1351 case 't': buf[len++] = '\t'; break; 1352 case '\\': buf[len++] = '\\'; break; 1353 case '"': buf[len++] = '"'; break; 1354 default: buf[len++] = *value; break; 1355 } 1356 value++; 1357 } 1358 } else { 1359 buf[len++] = *value++; 1360 } 1361 } 1362 buf[len] = '\0'; 1363 return 0; 1364 } else { 1365 len = strlen(value); 1366 if (len & 1) 1367 return -1; 1368 len /= 2; 1369 if (len >= buflen) 1370 len = buflen - 1; 1371 for (i = 0; i < len; i++) { 1372 char hex[3] = {value[i*2], value[i*2+1], 0}; 1373 char *end; 1374 long val = strtol(hex, &end, 16); 1375 if (*end) 1376 return -1; 1377 buf[i] = val; 1378 } 1379 buf[len] = '\0'; 1380 return 0; 1381 } 1382 } 1383 1384 static int nl80211_get_ssid(const char *ifname, char *buf) 1385 { 1386 char *res; 1387 struct nl80211_ssid_bssid sb = { .ssid = (unsigned char *)buf }; 1388 1389 /* try to find ssid from scan dump results */ 1390 res = nl80211_phy2ifname(ifname); 1391 sb.ssid[0] = 0; 1392 1393 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, 1394 nl80211_get_ssid_bssid_cb, &sb); 1395 1396 /* failed, try to find from hostapd info */ 1397 if (sb.ssid[0] == 0) 1398 nl80211_hostapd_query(ifname, "ssid", sb.ssid, 1399 IWINFO_ESSID_MAX_SIZE + 1); 1400 1401 if (sb.ssid[0] == 0) { 1402 /* ssid2 can be quoted, printf-encoded, or hex; needs parsing. 1403 * Buffer sized for printf encoding (\xHH = 4 chars per byte) plus P"" wrapper. */ 1404 char ssid2_raw[IWINFO_ESSID_MAX_SIZE * 4 + 3]; 1405 ssid2_raw[0] = 0; 1406 if (nl80211_hostapd_query(ifname, "ssid2", ssid2_raw, sizeof(ssid2_raw))) 1407 parse_ssid2(ssid2_raw, (char *)sb.ssid, IWINFO_ESSID_MAX_SIZE + 1); 1408 } 1409 1410 /* failed, try to obtain Mesh ID */ 1411 if (sb.ssid[0] == 0) 1412 iwinfo_ubus_query(res ? res : ifname, "mesh_id", 1413 buf, IWINFO_ESSID_MAX_SIZE + 1); 1414 1415 return (sb.ssid[0] == 0) ? -1 : 0; 1416 } 1417 1418 static int nl80211_get_bssid(const char *ifname, char *buf) 1419 { 1420 char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")]; 1421 struct nl80211_ssid_bssid sb = { }; 1422 1423 res = nl80211_phy2ifname(ifname); 1424 1425 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */ 1426 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1427 nl80211_get_macaddr_cb, &sb); 1428 1429 /* failed, try to find bssid from scan dump results */ 1430 if (sb.bssid[0] == 0) 1431 nl80211_request(res ? res : ifname, 1432 NL80211_CMD_GET_SCAN, NLM_F_DUMP, 1433 nl80211_get_ssid_bssid_cb, &sb); 1434 1435 /* failed, try to find mac from hostapd info */ 1436 if ((sb.bssid[0] == 0) && 1437 nl80211_hostapd_query(ifname, "bssid", bssid, sizeof(bssid))) 1438 { 1439 sb.bssid[0] = 1; 1440 sb.bssid[1] = strtol(&bssid[0], NULL, 16); 1441 sb.bssid[2] = strtol(&bssid[3], NULL, 16); 1442 sb.bssid[3] = strtol(&bssid[6], NULL, 16); 1443 sb.bssid[4] = strtol(&bssid[9], NULL, 16); 1444 sb.bssid[5] = strtol(&bssid[12], NULL, 16); 1445 sb.bssid[6] = strtol(&bssid[15], NULL, 16); 1446 } 1447 1448 if (sb.bssid[0]) 1449 { 1450 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", 1451 sb.bssid[1], sb.bssid[2], sb.bssid[3], 1452 sb.bssid[4], sb.bssid[5], sb.bssid[6]); 1453 1454 return 0; 1455 } 1456 1457 return -1; 1458 } 1459 1460 1461 static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg) 1462 { 1463 int *freq = arg; 1464 struct nlattr **attr = nl80211_parse(msg); 1465 struct nlattr *binfo[NL80211_BSS_MAX + 1]; 1466 1467 static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 1468 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, 1469 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 1470 }; 1471 1472 if (attr[NL80211_ATTR_BSS] && 1473 !nla_parse_nested(binfo, NL80211_BSS_MAX, 1474 attr[NL80211_ATTR_BSS], bss_policy)) 1475 { 1476 if (binfo[NL80211_BSS_STATUS] && binfo[NL80211_BSS_FREQUENCY]) 1477 *freq = nla_get_u32(binfo[NL80211_BSS_FREQUENCY]); 1478 } 1479 1480 return NL_SKIP; 1481 } 1482 1483 static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg) 1484 { 1485 int *freq = arg; 1486 struct nlattr **tb = nl80211_parse(msg); 1487 1488 if (tb[NL80211_ATTR_WIPHY_FREQ]) 1489 *freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]); 1490 1491 return NL_SKIP; 1492 } 1493 1494 static int nl80211_get_frequency(const char *ifname, int *buf) 1495 { 1496 char *res, channel[4] = { 0 }, hwmode[3] = { 0 }, opclass[4] = { 0 }; 1497 1498 /* try to find frequency from interface info */ 1499 res = nl80211_phy2ifname(ifname); 1500 *buf = 0; 1501 1502 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1503 nl80211_get_frequency_info_cb, buf); 1504 1505 /* failed, try to find frequency from hostapd info */ 1506 if ((*buf == 0) && 1507 nl80211_hostapd_query(ifname, "hw_mode", hwmode, sizeof(hwmode), 1508 "channel", channel, sizeof(channel), 1509 "op_class", opclass, sizeof(opclass)) >= 2) 1510 { 1511 *buf = nl80211_channel2freq(atoi(channel), hwmode, atoi(opclass)); 1512 } 1513 1514 /* failed, try to find frequency from scan results */ 1515 if (*buf == 0) 1516 { 1517 res = nl80211_phy2ifname(ifname); 1518 1519 nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, 1520 nl80211_get_frequency_scan_cb, buf); 1521 } 1522 1523 return (*buf == 0) ? -1 : 0; 1524 } 1525 1526 static int nl80211_get_center_freq1_cb(struct nl_msg *msg, void *arg) 1527 { 1528 int *freq = arg; 1529 struct nlattr **tb = nl80211_parse(msg); 1530 1531 if (tb[NL80211_ATTR_CENTER_FREQ1]) 1532 *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]); 1533 1534 return NL_SKIP; 1535 } 1536 1537 static int nl80211_get_center_freq1(const char *ifname, int *buf) 1538 { 1539 char *res; 1540 1541 /* try to find frequency from interface info */ 1542 res = nl80211_phy2ifname(ifname); 1543 *buf = 0; 1544 1545 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1546 nl80211_get_center_freq1_cb, buf); 1547 1548 return (*buf == 0) ? -1 : 0; 1549 } 1550 1551 static int nl80211_get_center_freq2_cb(struct nl_msg *msg, void *arg) 1552 { 1553 int *freq = arg; 1554 struct nlattr **tb = nl80211_parse(msg); 1555 1556 if (tb[NL80211_ATTR_CENTER_FREQ2]) 1557 *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]); 1558 1559 return NL_SKIP; 1560 } 1561 1562 static int nl80211_get_center_freq2(const char *ifname, int *buf) 1563 { 1564 char *res; 1565 1566 /* try to find frequency from interface info */ 1567 res = nl80211_phy2ifname(ifname); 1568 *buf = 0; 1569 1570 nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1571 nl80211_get_center_freq2_cb, buf); 1572 1573 return (*buf == 0) ? -1 : 0; 1574 } 1575 1576 static int nl80211_get_channel(const char *ifname, int *buf) 1577 { 1578 if (!nl80211_get_frequency(ifname, buf)) 1579 { 1580 *buf = nl80211_freq2channel(*buf); 1581 return 0; 1582 } 1583 1584 return -1; 1585 } 1586 1587 static int nl80211_get_center_chan1(const char *ifname, int *buf) 1588 { 1589 if (!nl80211_get_center_freq1(ifname, buf)) 1590 { 1591 *buf = nl80211_freq2channel(*buf); 1592 return 0; 1593 } 1594 1595 return -1; 1596 } 1597 1598 static int nl80211_get_center_chan2(const char *ifname, int *buf) 1599 { 1600 if (!nl80211_get_center_freq2(ifname, buf)) 1601 { 1602 *buf = nl80211_freq2channel(*buf); 1603 return 0; 1604 } 1605 1606 return -1; 1607 } 1608 1609 static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg) 1610 { 1611 int *buf = arg; 1612 struct nlattr **tb = nl80211_parse(msg); 1613 1614 if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]) 1615 *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])); 1616 1617 return NL_SKIP; 1618 } 1619 1620 static int nl80211_get_txpower(const char *ifname, int *buf) 1621 { 1622 char *res; 1623 1624 res = nl80211_phy2ifname(ifname); 1625 *buf = 0; 1626 1627 if (nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0, 1628 nl80211_get_txpower_cb, buf)) 1629 return -1; 1630 1631 return 0; 1632 } 1633 1634 1635 static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg) 1636 { 1637 int8_t dbm; 1638 int16_t mbit; 1639 struct nl80211_rssi_rate *rr = arg; 1640 struct nlattr **attr = nl80211_parse(msg); 1641 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; 1642 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; 1643 1644 static const struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { 1645 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, 1646 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, 1647 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, 1648 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 }, 1649 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 }, 1650 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, 1651 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, 1652 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, 1653 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, 1654 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, 1655 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, 1656 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, 1657 }; 1658 1659 static const struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { 1660 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, 1661 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, 1662 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, 1663 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, 1664 }; 1665 1666 if (attr[NL80211_ATTR_STA_INFO]) 1667 { 1668 if (!nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, 1669 attr[NL80211_ATTR_STA_INFO], stats_policy)) 1670 { 1671 if (sinfo[NL80211_STA_INFO_SIGNAL]) 1672 { 1673 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); 1674 rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1); 1675 rr->rssi_samples++; 1676 } 1677 1678 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) 1679 { 1680 if (!nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 1681 sinfo[NL80211_STA_INFO_TX_BITRATE], 1682 rate_policy)) 1683 { 1684 if (rinfo[NL80211_RATE_INFO_BITRATE]) 1685 { 1686 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]); 1687 rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1); 1688 rr->rate_samples++; 1689 } 1690 } 1691 } 1692 } 1693 } 1694 1695 return NL_SKIP; 1696 } 1697 1698 static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r) 1699 { 1700 DIR *d; 1701 struct dirent *de; 1702 1703 memset(r, 0, sizeof(*r)); 1704 1705 if ((d = opendir("/sys/class/net")) != NULL) 1706 { 1707 while ((de = readdir(d)) != NULL) 1708 { 1709 if (!strncmp(de->d_name, ifname, strlen(ifname)) && 1710 (!de->d_name[strlen(ifname)] || 1711 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) 1712 { 1713 nl80211_request(de->d_name, NL80211_CMD_GET_STATION, 1714 NLM_F_DUMP, nl80211_fill_signal_cb, r); 1715 } 1716 } 1717 1718 closedir(d); 1719 } 1720 } 1721 1722 static int nl80211_get_bitrate(const char *ifname, int *buf) 1723 { 1724 struct nl80211_rssi_rate rr; 1725 1726 nl80211_fill_signal(ifname, &rr); 1727 1728 if (rr.rate_samples) 1729 { 1730 *buf = (rr.rate * 100); 1731 return 0; 1732 } 1733 1734 return -1; 1735 } 1736 1737 static int nl80211_get_signal(const char *ifname, int *buf) 1738 { 1739 struct nl80211_rssi_rate rr; 1740 1741 nl80211_fill_signal(ifname, &rr); 1742 1743 if (rr.rssi_samples) 1744 { 1745 *buf = rr.rssi; 1746 return 0; 1747 } 1748 1749 return -1; 1750 } 1751 1752 struct nl80211_frequency_noise { 1753 uint32_t frequency; 1754 uint8_t noise; 1755 }; 1756 1757 static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg) 1758 { 1759 struct nl80211_frequency_noise *fn = arg; 1760 struct nlattr **tb = nl80211_parse(msg); 1761 struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1]; 1762 1763 static const struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = { 1764 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 1765 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 1766 }; 1767 1768 if (!tb[NL80211_ATTR_SURVEY_INFO]) 1769 return NL_SKIP; 1770 1771 if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX, 1772 tb[NL80211_ATTR_SURVEY_INFO], sp)) 1773 return NL_SKIP; 1774 1775 if (!si[NL80211_SURVEY_INFO_NOISE] || 1776 !si[NL80211_SURVEY_INFO_FREQUENCY]) 1777 return NL_SKIP; 1778 1779 if (nla_get_u32(si[NL80211_SURVEY_INFO_FREQUENCY]) != fn->frequency) 1780 return NL_SKIP; 1781 1782 if (!fn->noise || si[NL80211_SURVEY_INFO_IN_USE]) 1783 fn->noise = nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]); 1784 1785 return NL_SKIP; 1786 } 1787 1788 1789 static int nl80211_get_noise(const char *ifname, int *buf) 1790 { 1791 struct nl80211_frequency_noise fn = { }; 1792 1793 if (nl80211_get_frequency(ifname, (int *)&fn.frequency) < 0) 1794 goto out; 1795 1796 if (nl80211_request(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP, 1797 nl80211_get_noise_cb, &fn)) 1798 goto out; 1799 1800 *buf = (int8_t)fn.noise; 1801 return 0; 1802 1803 out: 1804 *buf = 0; 1805 return -1; 1806 } 1807 1808 static int nl80211_get_quality(const char *ifname, int *buf) 1809 { 1810 int signal; 1811 1812 if (!nl80211_get_signal(ifname, &signal)) 1813 { 1814 /* A positive signal level is usually just a quality 1815 * value, pass through as-is */ 1816 if (signal >= 0) 1817 { 1818 *buf = signal; 1819 } 1820 1821 /* The cfg80211 wext compat layer assumes a signal range 1822 * of -110 dBm to -40 dBm, the quality value is derived 1823 * by adding 110 to the signal level */ 1824 else 1825 { 1826 if (signal < -110) 1827 signal = -110; 1828 else if (signal > -40) 1829 signal = -40; 1830 1831 *buf = (signal + 110); 1832 } 1833 1834 return 0; 1835 } 1836 1837 return -1; 1838 } 1839 1840 static int nl80211_get_quality_max(const char *ifname, int *buf) 1841 { 1842 /* The cfg80211 wext compat layer assumes a maximum 1843 * quality of 70 */ 1844 *buf = 70; 1845 1846 return 0; 1847 } 1848 1849 static int nl80211_check_wepkey(const char *key) 1850 { 1851 if (key && *key) 1852 { 1853 switch (strlen(key)) 1854 { 1855 case 5: 1856 case 10: 1857 return IWINFO_CIPHER_WEP40; 1858 1859 case 13: 1860 case 26: 1861 return IWINFO_CIPHER_WEP104; 1862 } 1863 } 1864 1865 return 0; 1866 } 1867 1868 static const struct { 1869 const char *match; 1870 int version; 1871 int suite; 1872 } wpa_key_mgmt_strings[] = { 1873 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x }, 1874 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x }, 1875 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x }, 1876 { "EAP-SHA384", 4, IWINFO_KMGMT_8021x }, 1877 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x }, 1878 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK }, 1879 { "NONE", 0, IWINFO_KMGMT_NONE }, 1880 { "None", 0, IWINFO_KMGMT_NONE }, 1881 { "PSK", 0, IWINFO_KMGMT_PSK }, 1882 { "EAP", 0, IWINFO_KMGMT_8021x }, 1883 { "SAE", 4, IWINFO_KMGMT_SAE }, 1884 { "OWE", 4, IWINFO_KMGMT_OWE } 1885 }; 1886 1887 static void parse_wpa_suites(const char *str, int defversion, 1888 uint8_t *versions, uint8_t *suites) 1889 { 1890 size_t l; 1891 int i, version; 1892 const char *p, *q, *m, *sep = " \t\n,-+/"; 1893 1894 for (p = str; *p; ) 1895 { 1896 q = p; 1897 1898 for (i = 0; i < ARRAY_SIZE(wpa_key_mgmt_strings); i++) 1899 { 1900 m = wpa_key_mgmt_strings[i].match; 1901 l = strlen(m); 1902 1903 if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l]))) 1904 { 1905 if (wpa_key_mgmt_strings[i].version != 0) 1906 version = wpa_key_mgmt_strings[i].version; 1907 else 1908 version = defversion; 1909 1910 *versions |= version; 1911 *suites |= wpa_key_mgmt_strings[i].suite; 1912 1913 q += l; 1914 break; 1915 } 1916 } 1917 1918 if (q == p) 1919 q += strcspn(q, sep); 1920 1921 p = q + strspn(q, sep); 1922 } 1923 } 1924 1925 static const struct { 1926 const char *match; 1927 int cipher; 1928 } wpa_cipher_strings[] = { 1929 { "WEP-104", IWINFO_CIPHER_WEP104 }, 1930 { "WEP-40", IWINFO_CIPHER_WEP40 }, 1931 { "NONE", IWINFO_CIPHER_NONE }, 1932 { "TKIP", IWINFO_CIPHER_TKIP }, 1933 { "CCMP-256",IWINFO_CIPHER_CCMP256 }, 1934 { "CCMP", IWINFO_CIPHER_CCMP }, 1935 { "GCMP-256",IWINFO_CIPHER_GCMP256 }, 1936 { "GCMP", IWINFO_CIPHER_GCMP } 1937 }; 1938 1939 static void parse_wpa_ciphers(const char *str, uint16_t *ciphers) 1940 { 1941 int i; 1942 size_t l; 1943 const char *m, *p, *q, *sep = " \t\n,-+/"; 1944 1945 for (p = str; *p; ) 1946 { 1947 q = p; 1948 1949 for (i = 0; i < ARRAY_SIZE(wpa_cipher_strings); i++) 1950 { 1951 m = wpa_cipher_strings[i].match; 1952 l = strlen(m); 1953 1954 if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l]))) 1955 { 1956 *ciphers |= wpa_cipher_strings[i].cipher; 1957 1958 q += l; 1959 break; 1960 } 1961 } 1962 1963 if (q == p) 1964 q += strcspn(q, sep); 1965 1966 p = q + strspn(q, sep); 1967 } 1968 } 1969 1970 static int nl80211_get_encryption(const char *ifname, char *buf) 1971 { 1972 char *p; 1973 int opmode; 1974 uint8_t wpa_version = 0; 1975 char wpa[2], wpa_key_mgmt[64], wpa_pairwise[16], wpa_groupwise[16]; 1976 char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27]; 1977 char mode[16]; 1978 1979 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf; 1980 1981 /* WPA supplicant */ 1982 if (nl80211_wpactl_query(ifname, 1983 "pairwise_cipher", wpa_pairwise, sizeof(wpa_pairwise), 1984 "group_cipher", wpa_groupwise, sizeof(wpa_groupwise), 1985 "key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt), 1986 "mode", mode, sizeof(mode))) 1987 { 1988 /* WEP or Open */ 1989 if (!strcmp(wpa_key_mgmt, "NONE")) 1990 { 1991 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); 1992 parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers); 1993 1994 if (c->pair_ciphers != 0 && c->pair_ciphers != IWINFO_CIPHER_NONE) { 1995 c->enabled = 1; 1996 c->auth_suites = IWINFO_KMGMT_NONE; 1997 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; 1998 } 1999 else { 2000 c->pair_ciphers = 0; 2001 c->group_ciphers = 0; 2002 } 2003 } 2004 2005 /* MESH with SAE */ 2006 else if (!strcmp(mode, "mesh") && !strcmp(wpa_key_mgmt, "UNKNOWN")) 2007 { 2008 c->enabled = 1; 2009 c->wpa_version = 4; 2010 c->auth_suites = IWINFO_KMGMT_SAE; 2011 c->pair_ciphers = IWINFO_CIPHER_CCMP; 2012 c->group_ciphers = IWINFO_CIPHER_CCMP; 2013 } 2014 2015 /* WPA */ 2016 else 2017 { 2018 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); 2019 parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers); 2020 2021 p = wpa_key_mgmt; 2022 2023 if (!strncmp(p, "WPA2-", 5) || !strncmp(p, "WPA2/", 5)) 2024 { 2025 p += 5; 2026 wpa_version = 2; 2027 } 2028 else if (!strncmp(p, "WPA-", 4)) 2029 { 2030 p += 4; 2031 wpa_version = 1; 2032 } 2033 2034 parse_wpa_suites(p, wpa_version, &c->wpa_version, &c->auth_suites); 2035 2036 c->enabled = !!(c->wpa_version && c->auth_suites); 2037 } 2038 2039 return 0; 2040 } 2041 2042 /* Hostapd */ 2043 else if (nl80211_hostapd_query(ifname, 2044 "wpa", wpa, sizeof(wpa), 2045 "wpa_key_mgmt", wpa_key_mgmt, sizeof(wpa_key_mgmt), 2046 "wpa_pairwise", wpa_pairwise, sizeof(wpa_pairwise), 2047 "auth_algs", auth_algs, sizeof(auth_algs), 2048 "wep_key0", wep_key0, sizeof(wep_key0), 2049 "wep_key1", wep_key1, sizeof(wep_key1), 2050 "wep_key2", wep_key2, sizeof(wep_key2), 2051 "wep_key3", wep_key3, sizeof(wep_key3))) 2052 { 2053 c->wpa_version = 0; 2054 2055 if (wpa_key_mgmt[0]) 2056 { 2057 for (p = strtok(wpa_key_mgmt, " \t"); p != NULL; p = strtok(NULL, " \t")) 2058 { 2059 if (!strncmp(p, "WPA-", 4)) 2060 p += 4; 2061 2062 if (!strncmp(p, "FT-", 3)) 2063 p += 3; 2064 2065 parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites); 2066 } 2067 2068 c->enabled = c->wpa_version ? 1 : 0; 2069 } 2070 2071 if (wpa_pairwise[0]) 2072 parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers); 2073 2074 if (auth_algs[0]) 2075 { 2076 switch (atoi(auth_algs)) 2077 { 2078 case 1: 2079 c->auth_algs |= IWINFO_AUTH_OPEN; 2080 break; 2081 2082 case 2: 2083 c->auth_algs |= IWINFO_AUTH_SHARED; 2084 break; 2085 2086 case 3: 2087 c->auth_algs |= IWINFO_AUTH_OPEN; 2088 c->auth_algs |= IWINFO_AUTH_SHARED; 2089 break; 2090 } 2091 2092 c->pair_ciphers |= nl80211_check_wepkey(wep_key0); 2093 c->pair_ciphers |= nl80211_check_wepkey(wep_key1); 2094 c->pair_ciphers |= nl80211_check_wepkey(wep_key2); 2095 c->pair_ciphers |= nl80211_check_wepkey(wep_key3); 2096 2097 c->enabled = (c->auth_algs && c->pair_ciphers) ? 1 : 0; 2098 } 2099 2100 c->group_ciphers = c->pair_ciphers; 2101 2102 return 0; 2103 } 2104 2105 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */ 2106 else if (!nl80211_get_mode(ifname, &opmode) && 2107 (opmode == IWINFO_OPMODE_ADHOC || 2108 opmode == IWINFO_OPMODE_MESHPOINT)) 2109 { 2110 c->enabled = 0; 2111 2112 return 0; 2113 } 2114 2115 2116 return -1; 2117 } 2118 2119 static int nl80211_get_phyname(const char *ifname, char *buf) 2120 { 2121 const char *name; 2122 2123 name = nl80211_ifname2phy(ifname); 2124 2125 if (name) 2126 { 2127 strcpy(buf, name); 2128 return 0; 2129 } 2130 else if ((name = nl80211_phy2ifname(ifname)) != NULL) 2131 { 2132 name = nl80211_ifname2phy(name); 2133 2134 if (name) 2135 { 2136 strcpy(buf, ifname); 2137 return 0; 2138 } 2139 } 2140 2141 return -1; 2142 } 2143 2144 2145 static void nl80211_parse_rateinfo(struct nlattr **ri, 2146 struct iwinfo_rate_entry *re) 2147 { 2148 if (ri[NL80211_RATE_INFO_BITRATE32]) 2149 re->rate = nla_get_u32(ri[NL80211_RATE_INFO_BITRATE32]) * 100; 2150 else if (ri[NL80211_RATE_INFO_BITRATE]) 2151 re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100; 2152 2153 if (ri[NL80211_RATE_INFO_EHT_MCS]) 2154 { 2155 re->is_eht = 1; 2156 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_EHT_MCS]); 2157 2158 if (ri[NL80211_RATE_INFO_EHT_NSS]) 2159 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_EHT_NSS]); 2160 if (ri[NL80211_RATE_INFO_EHT_GI]) 2161 re->eht_gi = nla_get_u8(ri[NL80211_RATE_INFO_EHT_GI]); 2162 } 2163 else if (ri[NL80211_RATE_INFO_HE_MCS]) 2164 { 2165 re->is_he = 1; 2166 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_HE_MCS]); 2167 2168 if (ri[NL80211_RATE_INFO_HE_NSS]) 2169 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_HE_NSS]); 2170 if (ri[NL80211_RATE_INFO_HE_GI]) 2171 re->he_gi = nla_get_u8(ri[NL80211_RATE_INFO_HE_GI]); 2172 if (ri[NL80211_RATE_INFO_HE_DCM]) 2173 re->he_dcm = nla_get_u8(ri[NL80211_RATE_INFO_HE_DCM]); 2174 } 2175 else if (ri[NL80211_RATE_INFO_VHT_MCS]) 2176 { 2177 re->is_vht = 1; 2178 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]); 2179 2180 if (ri[NL80211_RATE_INFO_VHT_NSS]) 2181 re->nss = nla_get_u8(ri[NL80211_RATE_INFO_VHT_NSS]); 2182 } 2183 else if (ri[NL80211_RATE_INFO_MCS]) 2184 { 2185 re->is_ht = 1; 2186 re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_MCS]); 2187 } 2188 2189 if (ri[NL80211_RATE_INFO_5_MHZ_WIDTH]) 2190 re->mhz = 5; 2191 else if (ri[NL80211_RATE_INFO_10_MHZ_WIDTH]) 2192 re->mhz = 10; 2193 else if (ri[NL80211_RATE_INFO_40_MHZ_WIDTH]) 2194 re->mhz = 40; 2195 else if (ri[NL80211_RATE_INFO_80_MHZ_WIDTH]) 2196 re->mhz = 80; 2197 else if (ri[NL80211_RATE_INFO_80P80_MHZ_WIDTH] || 2198 ri[NL80211_RATE_INFO_160_MHZ_WIDTH]) 2199 re->mhz = 160; 2200 else if (ri[NL80211_RATE_INFO_320_MHZ_WIDTH]) 2201 re->mhz_hi = 320 / 256, re->mhz = 320 % 256; 2202 else 2203 re->mhz = 20; 2204 2205 if (ri[NL80211_RATE_INFO_SHORT_GI]) 2206 re->is_short_gi = 1; 2207 2208 re->is_40mhz = (re->mhz == 40); 2209 } 2210 2211 static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg) 2212 { 2213 struct nl80211_array_buf *arr = arg; 2214 struct iwinfo_survey_entry *e = arr->buf; 2215 struct nlattr **attr = nl80211_parse(msg); 2216 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; 2217 int rc; 2218 2219 static const struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { 2220 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, 2221 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, 2222 [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64 }, 2223 [NL80211_SURVEY_INFO_TIME_BUSY] = { .type = NLA_U64 }, 2224 [NL80211_SURVEY_INFO_TIME_EXT_BUSY] = { .type = NLA_U64 }, 2225 [NL80211_SURVEY_INFO_TIME_RX] = { .type = NLA_U64 }, 2226 [NL80211_SURVEY_INFO_TIME_TX] = { .type = NLA_U64 }, 2227 }; 2228 2229 rc = nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, 2230 attr[NL80211_ATTR_SURVEY_INFO], 2231 survey_policy); 2232 if (rc) 2233 return NL_SKIP; 2234 2235 /* advance to end of array */ 2236 e += arr->count; 2237 memset(e, 0, sizeof(*e)); 2238 2239 if (sinfo[NL80211_SURVEY_INFO_FREQUENCY]) 2240 e->mhz = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]); 2241 2242 if (sinfo[NL80211_SURVEY_INFO_NOISE]) 2243 e->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); 2244 2245 if (sinfo[NL80211_SURVEY_INFO_TIME]) 2246 e->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]); 2247 2248 if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY]) 2249 e->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]); 2250 2251 if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]) 2252 e->busy_time_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]); 2253 2254 if (sinfo[NL80211_SURVEY_INFO_TIME_RX]) 2255 e->rxtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]); 2256 2257 if (sinfo[NL80211_SURVEY_INFO_TIME_TX]) 2258 e->txtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]); 2259 2260 arr->count++; 2261 return NL_SKIP; 2262 } 2263 2264 2265 static void plink_state_to_str(char *dst, unsigned state) 2266 { 2267 switch (state) { 2268 case NL80211_PLINK_LISTEN: 2269 strcpy(dst, "LISTEN"); 2270 break; 2271 case NL80211_PLINK_OPN_SNT: 2272 strcpy(dst, "OPN_SNT"); 2273 break; 2274 case NL80211_PLINK_OPN_RCVD: 2275 strcpy(dst, "OPN_RCVD"); 2276 break; 2277 case NL80211_PLINK_CNF_RCVD: 2278 strcpy(dst, "CNF_RCVD"); 2279 break; 2280 case NL80211_PLINK_ESTAB: 2281 strcpy(dst, "ESTAB"); 2282 break; 2283 case NL80211_PLINK_HOLDING: 2284 strcpy(dst, "HOLDING"); 2285 break; 2286 case NL80211_PLINK_BLOCKED: 2287 strcpy(dst, "BLOCKED"); 2288 break; 2289 default: 2290 strcpy(dst, "UNKNOWN"); 2291 break; 2292 } 2293 } 2294 2295 static void power_mode_to_str(char *dst, struct nlattr *a) 2296 { 2297 enum nl80211_mesh_power_mode pm = nla_get_u32(a); 2298 2299 switch (pm) { 2300 case NL80211_MESH_POWER_ACTIVE: 2301 strcpy(dst, "ACTIVE"); 2302 break; 2303 case NL80211_MESH_POWER_LIGHT_SLEEP: 2304 strcpy(dst, "LIGHT SLEEP"); 2305 break; 2306 case NL80211_MESH_POWER_DEEP_SLEEP: 2307 strcpy(dst, "DEEP SLEEP"); 2308 break; 2309 default: 2310 strcpy(dst, "UNKNOWN"); 2311 break; 2312 } 2313 } 2314 2315 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg) 2316 { 2317 struct nl80211_array_buf *arr = arg; 2318 struct iwinfo_assoclist_entry *e = arr->buf; 2319 struct nlattr **attr = nl80211_parse(msg); 2320 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; 2321 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; 2322 struct nl80211_sta_flag_update *sta_flags; 2323 2324 static const struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { 2325 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, 2326 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, 2327 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, 2328 [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED }, 2329 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, 2330 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, 2331 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 }, 2332 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, 2333 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, 2334 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 }, 2335 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 }, 2336 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 }, 2337 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 }, 2338 [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32 }, 2339 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 }, 2340 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 }, 2341 [NL80211_STA_INFO_STA_FLAGS] = 2342 { .minlen = sizeof(struct nl80211_sta_flag_update) }, 2343 [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 }, 2344 /* mesh */ 2345 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, 2346 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, 2347 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, 2348 [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32 }, 2349 [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32 }, 2350 [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32 }, 2351 }; 2352 2353 static const struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { 2354 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, 2355 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, 2356 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, 2357 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, 2358 }; 2359 2360 /* advance to end of array */ 2361 e += arr->count; 2362 memset(e, 0, sizeof(*e)); 2363 2364 if (attr[NL80211_ATTR_MAC]) 2365 memcpy(e->mac, nla_data(attr[NL80211_ATTR_MAC]), 6); 2366 2367 if (attr[NL80211_ATTR_STA_INFO] && 2368 !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, 2369 attr[NL80211_ATTR_STA_INFO], stats_policy)) 2370 { 2371 if (sinfo[NL80211_STA_INFO_SIGNAL]) 2372 e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); 2373 2374 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG]) 2375 e->signal_avg = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]); 2376 2377 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME]) 2378 e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]); 2379 2380 if (sinfo[NL80211_STA_INFO_CONNECTED_TIME]) 2381 e->connected_time = nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]); 2382 2383 if (sinfo[NL80211_STA_INFO_RX_PACKETS]) 2384 e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]); 2385 2386 if (sinfo[NL80211_STA_INFO_TX_PACKETS]) 2387 e->tx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]); 2388 2389 if (sinfo[NL80211_STA_INFO_RX_BITRATE] && 2390 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 2391 sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy)) 2392 nl80211_parse_rateinfo(rinfo, &e->rx_rate); 2393 2394 if (sinfo[NL80211_STA_INFO_TX_BITRATE] && 2395 !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, 2396 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) 2397 nl80211_parse_rateinfo(rinfo, &e->tx_rate); 2398 2399 if (sinfo[NL80211_STA_INFO_RX_BYTES64]) 2400 e->rx_bytes = nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]); 2401 else if (sinfo[NL80211_STA_INFO_RX_BYTES]) 2402 e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]); 2403 2404 if (sinfo[NL80211_STA_INFO_TX_BYTES64]) 2405 e->tx_bytes = nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]); 2406 else if (sinfo[NL80211_STA_INFO_TX_BYTES]) 2407 e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]); 2408 2409 if (sinfo[NL80211_STA_INFO_TX_RETRIES]) 2410 e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]); 2411 2412 if (sinfo[NL80211_STA_INFO_TX_FAILED]) 2413 e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]); 2414 2415 if (sinfo[NL80211_STA_INFO_T_OFFSET]) 2416 e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]); 2417 2418 if (sinfo[NL80211_STA_INFO_RX_DROP_MISC]) 2419 e->rx_drop_misc = nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]); 2420 2421 if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]) 2422 e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]); 2423 2424 /* mesh */ 2425 if (sinfo[NL80211_STA_INFO_LLID]) 2426 e->llid = nla_get_u16(sinfo[NL80211_STA_INFO_LLID]); 2427 2428 if (sinfo[NL80211_STA_INFO_PLID]) 2429 e->plid = nla_get_u16(sinfo[NL80211_STA_INFO_PLID]); 2430 2431 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) 2432 plink_state_to_str(e->plink_state, 2433 nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])); 2434 2435 if (sinfo[NL80211_STA_INFO_LOCAL_PM]) 2436 power_mode_to_str(e->local_ps, sinfo[NL80211_STA_INFO_LOCAL_PM]); 2437 if (sinfo[NL80211_STA_INFO_PEER_PM]) 2438 power_mode_to_str(e->peer_ps, sinfo[NL80211_STA_INFO_PEER_PM]); 2439 if (sinfo[NL80211_STA_INFO_NONPEER_PM]) 2440 power_mode_to_str(e->nonpeer_ps, sinfo[NL80211_STA_INFO_NONPEER_PM]); 2441 2442 /* Station flags */ 2443 if (sinfo[NL80211_STA_INFO_STA_FLAGS]) 2444 { 2445 sta_flags = (struct nl80211_sta_flag_update *) 2446 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]); 2447 2448 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) && 2449 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED)) 2450 e->is_authorized = 1; 2451 2452 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) && 2453 sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) 2454 e->is_authenticated = 1; 2455 2456 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) && 2457 sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) 2458 e->is_preamble_short = 1; 2459 2460 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) && 2461 sta_flags->set & BIT(NL80211_STA_FLAG_WME)) 2462 e->is_wme = 1; 2463 2464 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) && 2465 sta_flags->set & BIT(NL80211_STA_FLAG_MFP)) 2466 e->is_mfp = 1; 2467 2468 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) && 2469 sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER)) 2470 e->is_tdls = 1; 2471 } 2472 } 2473 2474 e->noise = 0; /* filled in by caller */ 2475 arr->count++; 2476 2477 return NL_SKIP; 2478 } 2479 2480 static int nl80211_get_survey(const char *ifname, char *buf, int *len) 2481 { 2482 struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; 2483 int rc; 2484 2485 rc = nl80211_request(ifname, NL80211_CMD_GET_SURVEY, 2486 NLM_F_DUMP, nl80211_get_survey_cb, &arr); 2487 if (!rc) 2488 *len = (arr.count * sizeof(struct iwinfo_survey_entry)); 2489 else 2490 *len = 0; 2491 2492 return 0; 2493 } 2494 2495 static int nl80211_get_assoclist(const char *ifname, char *buf, int *len) 2496 { 2497 DIR *d; 2498 int i, noise = 0; 2499 struct dirent *de; 2500 struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; 2501 struct iwinfo_assoclist_entry *e; 2502 2503 if ((d = opendir("/sys/class/net")) != NULL) 2504 { 2505 while ((de = readdir(d)) != NULL) 2506 { 2507 if (!strncmp(de->d_name, ifname, strlen(ifname)) && 2508 (!de->d_name[strlen(ifname)] || 2509 !strncmp(&de->d_name[strlen(ifname)], ".sta", 4))) 2510 { 2511 nl80211_request(de->d_name, NL80211_CMD_GET_STATION, 2512 NLM_F_DUMP, nl80211_get_assoclist_cb, &arr); 2513 } 2514 } 2515 2516 closedir(d); 2517 2518 if (!nl80211_get_noise(ifname, &noise)) 2519 for (i = 0, e = arr.buf; i < arr.count; i++, e++) 2520 e->noise = noise; 2521 2522 *len = (arr.count * sizeof(struct iwinfo_assoclist_entry)); 2523 return 0; 2524 } 2525 2526 return -1; 2527 } 2528 2529 static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg) 2530 { 2531 int *dbm_max = arg; 2532 int ch_cur, ch_cmp, bands_remain, freqs_remain; 2533 2534 struct nlattr **attr = nl80211_parse(msg); 2535 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1]; 2536 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1]; 2537 struct nlattr *band, *freq; 2538 2539 static const struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { 2540 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, 2541 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, 2542 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, 2543 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, 2544 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, 2545 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 }, 2546 }; 2547 2548 ch_cur = *dbm_max; /* value int* is initialized with channel by caller */ 2549 *dbm_max = -1; 2550 2551 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) 2552 { 2553 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band), 2554 nla_len(band), NULL); 2555 2556 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain) 2557 { 2558 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, 2559 nla_data(freq), nla_len(freq), freq_policy); 2560 2561 ch_cmp = nl80211_freq2channel(nla_get_u32( 2562 freqs[NL80211_FREQUENCY_ATTR_FREQ])); 2563 2564 if ((!ch_cur || (ch_cmp == ch_cur)) && 2565 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) 2566 { 2567 *dbm_max = (int)(0.01 * nla_get_u32( 2568 freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])); 2569 2570 break; 2571 } 2572 } 2573 } 2574 2575 return NL_SKIP; 2576 } 2577 2578 static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len) 2579 { 2580 int err, ch_cur; 2581 int dbm_max = -1, dbm_cur, dbm_cnt; 2582 struct iwinfo_txpwrlist_entry entry; 2583 2584 if (nl80211_get_channel(ifname, &ch_cur)) 2585 ch_cur = 0; 2586 2587 /* initialize the value pointer with channel for callback */ 2588 dbm_max = ch_cur; 2589 2590 err = nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, 2591 nl80211_get_txpwrlist_cb, &dbm_max); 2592 2593 if (!err) 2594 { 2595 for (dbm_cur = 0, dbm_cnt = 0; 2596 dbm_cur < dbm_max; 2597 dbm_cur++, dbm_cnt++) 2598 { 2599 entry.dbm = dbm_cur; 2600 entry.mw = iwinfo_dbm2mw(dbm_cur); 2601 2602 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry)); 2603 } 2604 2605 entry.dbm = dbm_max; 2606 entry.mw = iwinfo_dbm2mw(dbm_max); 2607 2608 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry)); 2609 dbm_cnt++; 2610 2611 *len = dbm_cnt * sizeof(entry); 2612 return 0; 2613 } 2614 2615 return -1; 2616 } 2617 2618 static void nl80211_get_scancrypto(char *spec, struct iwinfo_crypto_entry *c) 2619 { 2620 int wpa_version = 0; 2621 char *p, *q, *proto, *suites; 2622 2623 c->enabled = 0; 2624 2625 for (p = strtok_r(spec, "[]", &q); p; p = strtok_r(NULL, "[]", &q)) { 2626 if (!strcmp(p, "WEP")) { 2627 c->enabled = 1; 2628 c->auth_suites = IWINFO_KMGMT_NONE; 2629 c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; 2630 c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104; 2631 break; 2632 } 2633 2634 proto = strtok(p, "-"); 2635 suites = strtok(NULL, "]"); 2636 2637 if (!proto || !suites) 2638 continue; 2639 2640 if (!strcmp(proto, "WPA2") || !strcmp(proto, "RSN")) 2641 wpa_version = 2; 2642 else if (!strcmp(proto, "WPA")) 2643 wpa_version = 1; 2644 else 2645 continue; 2646 2647 c->enabled = 1; 2648 2649 parse_wpa_suites(suites, wpa_version, &c->wpa_version, &c->auth_suites); 2650 parse_wpa_ciphers(suites, &c->pair_ciphers); 2651 } 2652 } 2653 2654 2655 struct nl80211_scanlist { 2656 struct iwinfo_scanlist_entry *e; 2657 int len; 2658 }; 2659 2660 2661 static void nl80211_get_scanlist_he_operation(unsigned char len, 2662 unsigned char *ie, struct iwinfo_scanlist_entry *e) 2663 { 2664 uint8_t offset = 6; 2665 2666 /* 6 GHz Operation Info not present */ 2667 if (!(ie[2] & 0x02)) 2668 return; 2669 2670 /* VHT Operation Info present */ 2671 if (ie[1] & 0x40) 2672 offset += 3; 2673 2674 /* Co Hosted BSS present */ 2675 if (ie[1] & 0x80) 2676 offset += 1; 2677 2678 if (len - offset < 5) 2679 return; 2680 2681 e->he_chan_info.chan_width = ie[offset + 1] & 0x03; 2682 e->he_chan_info.center_chan_1 = ie[offset + 2]; 2683 e->he_chan_info.center_chan_2 = ie[offset + 3]; 2684 } 2685 2686 2687 static void nl80211_get_scanlist_eht_operation(unsigned char len, 2688 unsigned char *ie, struct iwinfo_scanlist_entry *e) 2689 { 2690 /* EHT Operation Info not present */ 2691 if (!(ie[0] & 0x01)) 2692 return; 2693 2694 if (len < 8) 2695 return; 2696 2697 e->eht_chan_info.chan_width = ie[5] & 0x07; 2698 e->eht_chan_info.center_chan_1 = ie[6]; 2699 e->eht_chan_info.center_chan_2 = ie[7]; 2700 } 2701 2702 2703 static void nl80211_get_scanlist_extension(unsigned char len, 2704 unsigned char *ie, struct iwinfo_scanlist_entry *e) 2705 { 2706 if (len < 1) 2707 return; 2708 2709 switch (ie[0]) 2710 { 2711 case 36: /* HE Operation */ 2712 nl80211_get_scanlist_he_operation(len - 1, ie + 1, e); 2713 break; 2714 case 106: /* EHT Operation */ 2715 nl80211_get_scanlist_eht_operation(len - 1, ie + 1, e); 2716 break; 2717 } 2718 } 2719 2720 2721 static void nl80211_get_scanlist_ie(struct nlattr **bss, 2722 struct iwinfo_scanlist_entry *e) 2723 { 2724 int ielen = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 2725 unsigned char *ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); 2726 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 }; 2727 int len; 2728 2729 while (ielen >= 2 && ielen >= ie[1]) 2730 { 2731 switch (ie[0]) 2732 { 2733 case 0: /* SSID */ 2734 case 114: /* Mesh ID */ 2735 if (e->ssid[0] == 0) { 2736 len = min(ie[1], IWINFO_ESSID_MAX_SIZE); 2737 memcpy(e->ssid, ie + 2, len); 2738 e->ssid[len] = 0; 2739 } 2740 break; 2741 2742 case 48: /* RSN */ 2743 iwinfo_parse_rsn(&e->crypto, ie + 2, ie[1], 2744 IWINFO_CIPHER_CCMP, IWINFO_KMGMT_8021x); 2745 break; 2746 2747 case 221: /* Vendor */ 2748 if (ie[1] >= 4 && !memcmp(ie + 2, ms_oui, 3) && ie[5] == 1) 2749 iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4, 2750 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK); 2751 break; 2752 case 61: /* HT operation */ 2753 if (ie[1] >= 3) { 2754 e->ht_chan_info.primary_chan = ie[2]; 2755 e->ht_chan_info.secondary_chan_off = ie[3] & 0x3; 2756 e->ht_chan_info.chan_width = (ie[4] & 0x4)>>2; 2757 } 2758 break; 2759 case 192: /* VHT operation */ 2760 if (ie[1] >= 3) { 2761 e->vht_chan_info.chan_width = ie[2]; 2762 e->vht_chan_info.center_chan_1 = ie[3]; 2763 e->vht_chan_info.center_chan_2 = ie[4]; 2764 } 2765 break; 2766 case 255: /* Extension Tag*/ 2767 nl80211_get_scanlist_extension(ie[1], ie + 2, e); 2768 break; 2769 } 2770 2771 ielen -= ie[1] + 2; 2772 ie += ie[1] + 2; 2773 } 2774 } 2775 2776 static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg) 2777 { 2778 int8_t rssi; 2779 uint16_t caps; 2780 2781 struct nl80211_scanlist *sl = arg; 2782 struct nlattr **tb = nl80211_parse(msg); 2783 struct nlattr *bss[NL80211_BSS_MAX + 1]; 2784 2785 static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { 2786 [NL80211_BSS_TSF] = { .type = NLA_U64 }, 2787 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, 2788 [NL80211_BSS_BSSID] = { 0 }, 2789 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, 2790 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, 2791 [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 }, 2792 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, 2793 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, 2794 [NL80211_BSS_STATUS] = { .type = NLA_U32 }, 2795 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, 2796 [NL80211_BSS_BEACON_IES] = { 0 }, 2797 }; 2798 2799 if (!tb[NL80211_ATTR_BSS] || 2800 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], 2801 bss_policy) || 2802 !bss[NL80211_BSS_BSSID]) 2803 { 2804 return NL_SKIP; 2805 } 2806 2807 if (bss[NL80211_BSS_CAPABILITY]) 2808 caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); 2809 else 2810 caps = 0; 2811 2812 memset(sl->e, 0, sizeof(*sl->e)); 2813 memcpy(sl->e->mac, nla_data(bss[NL80211_BSS_BSSID]), 6); 2814 2815 if (caps & (1<<1)) 2816 sl->e->mode = IWINFO_OPMODE_ADHOC; 2817 else if (caps & (1<<0)) 2818 sl->e->mode = IWINFO_OPMODE_MASTER; 2819 else 2820 sl->e->mode = IWINFO_OPMODE_MESHPOINT; 2821 2822 if (caps & (1<<4)) 2823 sl->e->crypto.enabled = 1; 2824 2825 if (bss[NL80211_BSS_FREQUENCY]) 2826 { 2827 sl->e->mhz = nla_get_u32(bss[NL80211_BSS_FREQUENCY]); 2828 sl->e->band = nl80211_freq2band(sl->e->mhz); 2829 sl->e->channel = nl80211_freq2channel(sl->e->mhz); 2830 } 2831 2832 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) 2833 nl80211_get_scanlist_ie(bss, sl->e); 2834 2835 if (bss[NL80211_BSS_SIGNAL_MBM]) 2836 { 2837 sl->e->signal = 2838 (uint8_t)((int32_t)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100); 2839 2840 rssi = sl->e->signal - 0x100; 2841 2842 if (rssi < -110) 2843 rssi = -110; 2844 else if (rssi > -40) 2845 rssi = -40; 2846 2847 sl->e->quality = (rssi + 110); 2848 sl->e->quality_max = 70; 2849 } 2850 2851 if (sl->e->crypto.enabled && !sl->e->crypto.wpa_version) 2852 { 2853 sl->e->crypto.auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED; 2854 sl->e->crypto.pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104; 2855 } 2856 2857 sl->e++; 2858 sl->len++; 2859 2860 return NL_SKIP; 2861 } 2862 2863 static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len) 2864 { 2865 struct nl80211_scanlist sl = { .e = (struct iwinfo_scanlist_entry *)buf }; 2866 2867 if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL)) 2868 goto out; 2869 2870 if (nl80211_wait("nl80211", "scan", 2871 NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED)) 2872 goto out; 2873 2874 if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP, 2875 nl80211_get_scanlist_cb, &sl)) 2876 goto out; 2877 2878 *len = sl.len * sizeof(struct iwinfo_scanlist_entry); 2879 return 0; 2880 2881 out: 2882 *len = 0; 2883 return -1; 2884 } 2885 2886 static int wpasupp_ssid_decode(const char *in, char *out, int outlen) 2887 { 2888 #define hex(x) \ 2889 (((x) >= 'a') ? ((x) - 'a' + 10) : \ 2890 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - ''))) 2891 2892 int len = 0; 2893 2894 while (*in) 2895 { 2896 if (len + 1 >= outlen) 2897 break; 2898 2899 switch (*in) 2900 { 2901 case '\\': 2902 in++; 2903 switch (*in) 2904 { 2905 case 'n': 2906 out[len++] = '\n'; in++; 2907 break; 2908 2909 case 'r': 2910 out[len++] = '\r'; in++; 2911 break; 2912 2913 case 't': 2914 out[len++] = '\t'; in++; 2915 break; 2916 2917 case 'e': 2918 out[len++] = '\033'; in++; 2919 break; 2920 2921 case 'x': 2922 if (isxdigit(*(in+1)) && isxdigit(*(in+2))) 2923 out[len++] = hex(*(in+1)) * 16 + hex(*(in+2)); 2924 in += 3; 2925 break; 2926 2927 default: 2928 out[len++] = *in++; 2929 break; 2930 } 2931 break; 2932 2933 default: 2934 out[len++] = *in++; 2935 break; 2936 } 2937 } 2938 2939 if (outlen > len) 2940 out[len] = '\0'; 2941 2942 return len; 2943 } 2944 2945 static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len) 2946 { 2947 int sock, qmax, rssi, tries, count = -1, ready = 0; 2948 char *pos, *line, *bssid, *freq, *signal, *flags, *ssid, reply[4096]; 2949 struct sockaddr_un local = { 0 }; 2950 struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf; 2951 2952 sock = nl80211_wpactl_connect(ifname, &local); 2953 2954 if (sock < 0) 2955 return sock; 2956 2957 send(sock, "ATTACH", 6, 0); 2958 send(sock, "SCAN", 4, 0); 2959 2960 /* 2961 * wait for scan results: 2962 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan 2963 * 72 channels at most. We'll also receive two "OK" messages acknowledging 2964 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra 2965 * time to process the results, so try 72 + 2 + 1 times. 2966 */ 2967 for (tries = 0; tries < 75; tries++) 2968 { 2969 if (nl80211_wpactl_recv(sock, reply, sizeof(reply)) <= 0) 2970 continue; 2971 2972 /* got an event notification */ 2973 if (reply[0] == '<') 2974 { 2975 /* scan results are ready */ 2976 if (strstr(reply, "CTRL-EVENT-SCAN-RESULTS")) 2977 { 2978 /* send "SCAN_RESULTS" command */ 2979 ready = (send(sock, "SCAN_RESULTS", 12, 0) == 12); 2980 break; 2981 } 2982 2983 /* is another unrelated event, retry */ 2984 tries--; 2985 } 2986 2987 /* scanning already in progress, keep awaiting results */ 2988 else if (!strcmp(reply, "FAIL-BUSY\n")) 2989 { 2990 tries--; 2991 } 2992 2993 /* another failure, abort */ 2994 else if (!strncmp(reply, "FAIL-", 5)) 2995 { 2996 break; 2997 } 2998 } 2999 3000 /* receive and parse scan results if the wait above didn't time out */ 3001 while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0) 3002 { 3003 /* received an event notification, receive again */ 3004 if (reply[0] == '<') 3005 continue; 3006 3007 nl80211_get_quality_max(ifname, &qmax); 3008 3009 for (line = strtok_r(reply, "\n", &pos); 3010 line != NULL; 3011 line = strtok_r(NULL, "\n", &pos)) 3012 { 3013 /* skip header line */ 3014 if (count < 0) 3015 { 3016 count++; 3017 continue; 3018 } 3019 3020 bssid = strtok(line, "\t"); 3021 freq = strtok(NULL, "\t"); 3022 signal = strtok(NULL, "\t"); 3023 flags = strtok(NULL, "\t"); 3024 ssid = strtok(NULL, "\n"); 3025 3026 if (!bssid || !freq || !signal || !flags) 3027 continue; 3028 3029 /* BSSID */ 3030 e->mac[0] = strtol(&bssid[0], NULL, 16); 3031 e->mac[1] = strtol(&bssid[3], NULL, 16); 3032 e->mac[2] = strtol(&bssid[6], NULL, 16); 3033 e->mac[3] = strtol(&bssid[9], NULL, 16); 3034 e->mac[4] = strtol(&bssid[12], NULL, 16); 3035 e->mac[5] = strtol(&bssid[15], NULL, 16); 3036 3037 /* SSID */ 3038 if (ssid) 3039 wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid)); 3040 else 3041 e->ssid[0] = 0; 3042 3043 /* Mode */ 3044 if (strstr(flags, "[MESH]")) 3045 e->mode = IWINFO_OPMODE_MESHPOINT; 3046 else if (strstr(flags, "[IBSS]")) 3047 e->mode = IWINFO_OPMODE_ADHOC; 3048 else 3049 e->mode = IWINFO_OPMODE_MASTER; 3050 3051 /* Channel */ 3052 e->mhz = atoi(freq); 3053 e->band = nl80211_freq2band(e->mhz); 3054 e->channel = nl80211_freq2channel(e->mhz); 3055 3056 /* Signal */ 3057 rssi = atoi(signal); 3058 e->signal = rssi; 3059 3060 /* Quality */ 3061 if (rssi < 0) 3062 { 3063 /* The cfg80211 wext compat layer assumes a signal range 3064 * of -110 dBm to -40 dBm, the quality value is derived 3065 * by adding 110 to the signal level */ 3066 if (rssi < -110) 3067 rssi = -110; 3068 else if (rssi > -40) 3069 rssi = -40; 3070 3071 e->quality = (rssi + 110); 3072 } 3073 else 3074 { 3075 e->quality = rssi; 3076 } 3077 3078 /* Max. Quality */ 3079 e->quality_max = qmax; 3080 3081 /* Crypto */ 3082 nl80211_get_scancrypto(flags, &e->crypto); 3083 3084 count++; 3085 e++; 3086 } 3087 3088 *len = count * sizeof(struct iwinfo_scanlist_entry); 3089 break; 3090 } 3091 3092 close(sock); 3093 unlink(local.sun_path); 3094 3095 return (count >= 0) ? 0 : -1; 3096 } 3097 3098 static int nl80211_get_scanlist(const char *ifname, char *buf, int *len) 3099 { 3100 char *res; 3101 int rv, mode; 3102 3103 *len = 0; 3104 3105 /* Got a radioX pseudo interface, find some interface on it or create one */ 3106 if (!strncmp(ifname, "radio", 5)) 3107 { 3108 /* Reuse existing interface */ 3109 if ((res = nl80211_phy2ifname(ifname)) != NULL) 3110 { 3111 return nl80211_get_scanlist(res, buf, len); 3112 } 3113 3114 /* Need to spawn a temporary iface for scanning */ 3115 else if ((res = nl80211_ifadd(ifname)) != NULL) 3116 { 3117 rv = nl80211_get_scanlist(res, buf, len); 3118 nl80211_ifdel(res); 3119 return rv; 3120 } 3121 } 3122 3123 /* WPA supplicant */ 3124 if (!nl80211_get_scanlist_wpactl(ifname, buf, len)) 3125 { 3126 return 0; 3127 } 3128 3129 /* station / ad-hoc / monitor scan */ 3130 else if (!nl80211_get_mode(ifname, &mode) && 3131 (mode == IWINFO_OPMODE_ADHOC || 3132 mode == IWINFO_OPMODE_MASTER || 3133 mode == IWINFO_OPMODE_CLIENT || 3134 mode == IWINFO_OPMODE_MONITOR) && 3135 iwinfo_ifup(ifname)) 3136 { 3137 return nl80211_get_scanlist_nl(ifname, buf, len); 3138 } 3139 3140 /* AP scan */ 3141 else 3142 { 3143 /* Got a temp interface, don't create yet another one */ 3144 if (!strncmp(ifname, "tmp.", 4)) 3145 { 3146 if (!iwinfo_ifup(ifname)) 3147 return -1; 3148 3149 rv = nl80211_get_scanlist_nl(ifname, buf, len); 3150 iwinfo_ifdown(ifname); 3151 return rv; 3152 } 3153 3154 /* Spawn a new scan interface */ 3155 else 3156 { 3157 if (!(res = nl80211_ifadd(ifname))) 3158 return -1; 3159 3160 iwinfo_ifmac(res); 3161 3162 /* if we can take the new interface up, the driver supports an 3163 * additional interface and there's no need to tear down the ap */ 3164 if (iwinfo_ifup(res)) 3165 { 3166 rv = nl80211_get_scanlist_nl(res, buf, len); 3167 iwinfo_ifdown(res); 3168 } 3169 3170 /* driver cannot create secondary interface, take down ap 3171 * during scan */ 3172 else if (iwinfo_ifdown(ifname) && iwinfo_ifup(res)) 3173 { 3174 rv = nl80211_get_scanlist_nl(res, buf, len); 3175 iwinfo_ifdown(res); 3176 iwinfo_ifup(ifname); 3177 nl80211_hostapd_hup(ifname); 3178 } 3179 else 3180 rv = -1; 3181 3182 nl80211_ifdel(res); 3183 return rv; 3184 } 3185 } 3186 3187 return -1; 3188 } 3189 3190 static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg) 3191 { 3192 int bands_remain, freqs_remain; 3193 3194 struct nl80211_array_buf *arr = arg; 3195 struct iwinfo_freqlist_entry *e; 3196 3197 struct nlattr **attr = nl80211_parse(msg); 3198 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1]; 3199 struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1]; 3200 struct nlattr *band, *freq; 3201 3202 e = arr->buf; 3203 e += arr->count; 3204 3205 if (attr[NL80211_ATTR_WIPHY_BANDS]) { 3206 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) 3207 { 3208 nla_parse(bands, NL80211_BAND_ATTR_MAX, 3209 nla_data(band), nla_len(band), NULL); 3210 3211 if (bands[NL80211_BAND_ATTR_FREQS]) { 3212 nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS], freqs_remain) 3213 { 3214 nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX, 3215 nla_data(freq), nla_len(freq), NULL); 3216 3217 if (!freqs[NL80211_FREQUENCY_ATTR_FREQ] || 3218 freqs[NL80211_FREQUENCY_ATTR_DISABLED]) 3219 continue; 3220 3221 e->band = nl80211_get_band(band->nla_type); 3222 e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]); 3223 e->channel = nl80211_freq2channel(e->mhz); 3224 3225 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS]) 3226 e->flags |= IWINFO_FREQ_NO_HT40MINUS; 3227 if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS]) 3228 e->flags |= IWINFO_FREQ_NO_HT40PLUS; 3229 if (freqs[NL80211_FREQUENCY_ATTR_NO_80MHZ]) 3230 e->flags |= IWINFO_FREQ_NO_80MHZ; 3231 if (freqs[NL80211_FREQUENCY_ATTR_NO_160MHZ]) 3232 e->flags |= IWINFO_FREQ_NO_160MHZ; 3233 if (freqs[NL80211_FREQUENCY_ATTR_NO_20MHZ]) 3234 e->flags |= IWINFO_FREQ_NO_20MHZ; 3235 if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ]) 3236 e->flags |= IWINFO_FREQ_NO_10MHZ; 3237 if (freqs[NL80211_FREQUENCY_ATTR_NO_HE]) 3238 e->flags |= IWINFO_FREQ_NO_HE; 3239 if (freqs[NL80211_FREQUENCY_ATTR_NO_IR] && 3240 !freqs[NL80211_FREQUENCY_ATTR_RADAR]) 3241 e->flags |= IWINFO_FREQ_NO_IR; 3242 if (freqs[NL80211_FREQUENCY_ATTR_INDOOR_ONLY]) 3243 e->flags |= IWINFO_FREQ_INDOOR_ONLY; 3244 3245 /* keep backwards compatibility */ 3246 e->restricted = (e->flags & IWINFO_FREQ_NO_IR) ? 1 : 0; 3247 3248 e++; 3249 arr->count++; 3250 } 3251 } 3252 } 3253 } 3254 3255 return NL_SKIP; 3256 } 3257 3258 static int nl80211_get_freqlist(const char *ifname, char *buf, int *len) 3259 { 3260 struct nl80211_msg_conveyor *cv; 3261 struct nl80211_array_buf arr = { .buf = buf, .count = 0 }; 3262 uint32_t features = nl80211_get_protocol_features(ifname); 3263 int flags; 3264 3265 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0; 3266 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags); 3267 if (!cv) 3268 goto out; 3269 3270 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 3271 if (nl80211_send(cv, nl80211_get_freqlist_cb, &arr)) 3272 goto out; 3273 3274 *len = arr.count * sizeof(struct iwinfo_freqlist_entry); 3275 return 0; 3276 3277 nla_put_failure: 3278 nl80211_free(cv); 3279 out: 3280 *len = 0; 3281 return -1; 3282 } 3283 3284 static int nl80211_get_country_cb(struct nl_msg *msg, void *arg) 3285 { 3286 char *buf = arg; 3287 struct nlattr **attr = nl80211_parse(msg); 3288 3289 if (attr[NL80211_ATTR_REG_ALPHA2]) 3290 memcpy(buf, nla_data(attr[NL80211_ATTR_REG_ALPHA2]), 2); 3291 else 3292 buf[0] = 0; 3293 3294 return NL_SKIP; 3295 } 3296 3297 static int nl80211_get_country(const char *ifname, char *buf) 3298 { 3299 if (nl80211_request(ifname, NL80211_CMD_GET_REG, 0, 3300 nl80211_get_country_cb, buf)) 3301 return -1; 3302 3303 return 0; 3304 } 3305 3306 static int nl80211_get_countrylist(const char *ifname, char *buf, int *len) 3307 { 3308 int count; 3309 struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf; 3310 const struct iwinfo_iso3166_label *l; 3311 3312 for (l = IWINFO_ISO3166_NAMES, count = 0; l->iso3166; l++, e++, count++) 3313 { 3314 e->iso3166 = l->iso3166; 3315 e->ccode[0] = (l->iso3166 / 256); 3316 e->ccode[1] = (l->iso3166 % 256); 3317 e->ccode[2] = 0; 3318 } 3319 3320 *len = (count * sizeof(struct iwinfo_country_entry)); 3321 return 0; 3322 } 3323 3324 3325 struct nl80211_modes 3326 { 3327 bool ok; 3328 uint32_t hw; 3329 uint32_t ht; 3330 3331 uint8_t bands; 3332 3333 uint16_t nl_ht; 3334 uint32_t nl_vht; 3335 uint16_t he_phy_cap[6]; 3336 uint16_t eht_phy_cap[9]; 3337 }; 3338 3339 static void nl80211_eval_modelist(struct nl80211_modes *m) 3340 { 3341 /* Treat any nonzero capability as 11n */ 3342 if (m->nl_ht > 0) 3343 { 3344 m->hw |= IWINFO_80211_N; 3345 m->ht |= IWINFO_HTMODE_HT20; 3346 3347 if (m->nl_ht & (1 << 1)) 3348 m->ht |= IWINFO_HTMODE_HT40; 3349 } 3350 3351 if (m->he_phy_cap[0] != 0) { 3352 m->hw |= IWINFO_80211_AX; 3353 m->ht |= IWINFO_HTMODE_HE20; 3354 3355 if (m->he_phy_cap[0] & BIT(9)) 3356 m->ht |= IWINFO_HTMODE_HE40; 3357 if (m->he_phy_cap[0] & BIT(10)) 3358 m->ht |= IWINFO_HTMODE_HE40 | IWINFO_HTMODE_HE80; 3359 if (m->he_phy_cap[0] & BIT(11)) 3360 m->ht |= IWINFO_HTMODE_HE160; 3361 if (m->he_phy_cap[0] & BIT(12)) 3362 m->ht |= IWINFO_HTMODE_HE160 | IWINFO_HTMODE_HE80_80; 3363 } 3364 3365 if (m->eht_phy_cap[0] != 0) { 3366 m->hw |= IWINFO_80211_BE; 3367 m->ht |= IWINFO_HTMODE_EHT20; 3368 3369 if (m->he_phy_cap[0] & BIT(9)) 3370 m->ht |= IWINFO_HTMODE_EHT40; 3371 if (m->he_phy_cap[0] & BIT(10)) 3372 m->ht |= IWINFO_HTMODE_EHT40 | IWINFO_HTMODE_EHT80; 3373 if (m->he_phy_cap[0] & BIT(11)) 3374 m->ht |= IWINFO_HTMODE_EHT160; 3375 if (m->he_phy_cap[0] & BIT(12)) 3376 m->ht |= IWINFO_HTMODE_EHT160 | IWINFO_HTMODE_EHT80_80; 3377 if ((m->eht_phy_cap[0] & BIT(9)) && (m->bands & IWINFO_BAND_6)) 3378 m->ht |= IWINFO_HTMODE_EHT320; 3379 } 3380 3381 if (m->bands & IWINFO_BAND_24) 3382 { 3383 m->hw |= IWINFO_80211_B; 3384 m->hw |= IWINFO_80211_G; 3385 } 3386 3387 if (m->bands & IWINFO_BAND_5) 3388 { 3389 /* Treat any nonzero capability as 11ac */ 3390 if (m->nl_vht > 0) 3391 { 3392 m->hw |= IWINFO_80211_AC; 3393 m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80; 3394 3395 switch ((m->nl_vht >> 2) & 3) 3396 { 3397 case 2: 3398 m->ht |= IWINFO_HTMODE_VHT80_80; 3399 /* fall through */ 3400 3401 case 1: 3402 m->ht |= IWINFO_HTMODE_VHT160; 3403 } 3404 } 3405 else 3406 { 3407 m->hw |= IWINFO_80211_A; 3408 } 3409 } 3410 3411 if (m->bands & IWINFO_BAND_60) 3412 { 3413 m->hw |= IWINFO_80211_AD; 3414 } 3415 3416 } 3417 3418 static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg) 3419 { 3420 struct nl80211_modes *m = arg; 3421 int bands_remain; 3422 struct nlattr **attr = nl80211_parse(msg); 3423 struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1]; 3424 struct nlattr *band; 3425 3426 if (attr[NL80211_ATTR_WIPHY_BANDS]) 3427 { 3428 nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain) 3429 { 3430 m->bands |= nl80211_get_band(band->nla_type); 3431 3432 nla_parse(bands, NL80211_BAND_ATTR_MAX, 3433 nla_data(band), nla_len(band), NULL); 3434 3435 if (bands[NL80211_BAND_ATTR_HT_CAPA]) 3436 m->nl_ht = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]); 3437 3438 if (bands[NL80211_BAND_ATTR_VHT_CAPA]) 3439 m->nl_vht = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]); 3440 3441 if (bands[NL80211_BAND_ATTR_IFTYPE_DATA]) { 3442 struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1]; 3443 struct nlattr *nl_iftype; 3444 int rem_band; 3445 int len; 3446 3447 nla_for_each_nested(nl_iftype, bands[NL80211_BAND_ATTR_IFTYPE_DATA], rem_band) { 3448 nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX, 3449 nla_data(nl_iftype), nla_len(nl_iftype), NULL); 3450 3451 // HE 3452 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) { 3453 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]); 3454 3455 if (len > sizeof(m->he_phy_cap) - 1) 3456 len = sizeof(m->he_phy_cap) - 1; 3457 memcpy(&((__u8 *)m->he_phy_cap)[1], 3458 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]), 3459 len); 3460 } 3461 3462 // EHT 3463 if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]) { 3464 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]); 3465 3466 if (len > sizeof(m->eht_phy_cap) - 1) 3467 len = sizeof(m->eht_phy_cap) - 1; 3468 memcpy(&((uint8_t *)m->eht_phy_cap)[1], 3469 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]), 3470 len); 3471 } 3472 } 3473 } 3474 } 3475 3476 m->ok = 1; 3477 } 3478 3479 return NL_SKIP; 3480 } 3481 3482 static int nl80211_get_hwmodelist(const char *ifname, int *buf) 3483 { 3484 struct nl80211_msg_conveyor *cv; 3485 struct nl80211_modes m = {}; 3486 uint32_t features = nl80211_get_protocol_features(ifname); 3487 int flags; 3488 3489 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0; 3490 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags); 3491 if (!cv) 3492 goto out; 3493 3494 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 3495 if (nl80211_send(cv, nl80211_get_modelist_cb, &m)) 3496 goto nla_put_failure; 3497 3498 nl80211_eval_modelist(&m); 3499 3500 *buf = m.hw; 3501 3502 return 0; 3503 3504 nla_put_failure: 3505 nl80211_free(cv); 3506 out: 3507 return -1; 3508 } 3509 3510 struct chan_info { 3511 int width; 3512 int mode; 3513 }; 3514 3515 static int nl80211_get_htmode_cb(struct nl_msg *msg, void *arg) 3516 { 3517 struct nlattr **tb = nl80211_parse(msg); 3518 struct nlattr *cur; 3519 struct chan_info *chn = arg; 3520 3521 if ((cur = tb[NL80211_ATTR_CHANNEL_WIDTH])) 3522 chn->width = nla_get_u32(cur); 3523 3524 if ((cur = tb[NL80211_ATTR_BSS_HT_OPMODE])) 3525 chn->mode = nla_get_u32(cur); 3526 3527 return NL_SKIP; 3528 } 3529 3530 static int nl80211_get_htmode(const char *ifname, int *buf) 3531 { 3532 struct chan_info chn = { 0 }; 3533 char *res, b[2] = { 0 }; 3534 int err; 3535 bool he = false; 3536 bool eht = false; 3537 3538 res = nl80211_phy2ifname(ifname); 3539 *buf = 0; 3540 3541 err = nl80211_request(res ? res : ifname, 3542 NL80211_CMD_GET_INTERFACE, 0, 3543 nl80211_get_htmode_cb, &chn); 3544 if (err) 3545 return -1; 3546 3547 if (nl80211_hostapd_query(res ? res : ifname, "ieee80211be", b, sizeof(b))) 3548 eht = b[0] == '1'; 3549 3550 if (nl80211_hostapd_query(res ? res : ifname, "ieee80211ax", b, sizeof(b))) 3551 he = b[0] == '1'; 3552 else if (nl80211_wpactl_query(res ? res : ifname, "wifi_generation", b, sizeof(b))) { 3553 he = b[0] == '6'; 3554 eht = b[0] == '7'; 3555 } 3556 3557 switch (chn.width) { 3558 case NL80211_CHAN_WIDTH_20: 3559 if (eht) 3560 *buf = IWINFO_HTMODE_EHT20; 3561 else if (he) 3562 *buf = IWINFO_HTMODE_HE20; 3563 else if (chn.mode == -1) 3564 *buf = IWINFO_HTMODE_VHT20; 3565 else 3566 *buf = IWINFO_HTMODE_HT20; 3567 break; 3568 case NL80211_CHAN_WIDTH_40: 3569 if (eht) 3570 *buf = IWINFO_HTMODE_EHT40; 3571 else if (he) 3572 *buf = IWINFO_HTMODE_HE40; 3573 else if (chn.mode == -1) 3574 *buf = IWINFO_HTMODE_VHT40; 3575 else 3576 *buf = IWINFO_HTMODE_HT40; 3577 break; 3578 case NL80211_CHAN_WIDTH_80: 3579 if (eht) 3580 *buf = IWINFO_HTMODE_EHT80; 3581 else if (he) 3582 *buf = IWINFO_HTMODE_HE80; 3583 else 3584 *buf = IWINFO_HTMODE_VHT80; 3585 break; 3586 case NL80211_CHAN_WIDTH_80P80: 3587 if (eht) 3588 *buf = IWINFO_HTMODE_EHT80_80; 3589 else if (he) 3590 *buf = IWINFO_HTMODE_HE80_80; 3591 else 3592 *buf = IWINFO_HTMODE_VHT80_80; 3593 break; 3594 case NL80211_CHAN_WIDTH_160: 3595 if (eht) 3596 *buf = IWINFO_HTMODE_EHT160; 3597 else if (he) 3598 *buf = IWINFO_HTMODE_HE160; 3599 else 3600 *buf = IWINFO_HTMODE_VHT160; 3601 break; 3602 case NL80211_CHAN_WIDTH_320: 3603 *buf = IWINFO_HTMODE_EHT320; 3604 break; 3605 case NL80211_CHAN_WIDTH_5: 3606 case NL80211_CHAN_WIDTH_10: 3607 case NL80211_CHAN_WIDTH_20_NOHT: 3608 *buf = IWINFO_HTMODE_NOHT; 3609 break; 3610 default: 3611 return -1; 3612 } 3613 3614 return 0; 3615 } 3616 3617 static int nl80211_get_htmodelist(const char *ifname, int *buf) 3618 { 3619 struct nl80211_msg_conveyor *cv; 3620 struct nl80211_modes m = {}; 3621 uint32_t features = nl80211_get_protocol_features(ifname); 3622 int flags; 3623 3624 flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0; 3625 cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags); 3626 if (!cv) 3627 goto out; 3628 3629 NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP); 3630 if (nl80211_send(cv, nl80211_get_modelist_cb, &m)) 3631 goto nla_put_failure; 3632 3633 nl80211_eval_modelist(&m); 3634 3635 *buf = m.ht; 3636 3637 return 0; 3638 3639 nla_put_failure: 3640 nl80211_free(cv); 3641 out: 3642 return -1; 3643 } 3644 3645 3646 static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg) 3647 { 3648 struct nlattr **attr = nl80211_parse(msg); 3649 struct nlattr *comb; 3650 int *ret = arg; 3651 int comb_rem, limit_rem, mode_rem; 3652 3653 *ret = 0; 3654 if (!attr[NL80211_ATTR_INTERFACE_COMBINATIONS]) 3655 return NL_SKIP; 3656 3657 nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem) 3658 { 3659 static const struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = { 3660 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED }, 3661 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 }, 3662 }; 3663 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1]; 3664 static const struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = { 3665 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED }, 3666 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 }, 3667 }; 3668 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT+1]; 3669 struct nlattr *limit; 3670 3671 nla_parse_nested(tb_comb, NUM_NL80211_IFACE_COMB, comb, iface_combination_policy); 3672 3673 if (!tb_comb[NL80211_IFACE_COMB_LIMITS]) 3674 continue; 3675 3676 nla_for_each_nested(limit, tb_comb[NL80211_IFACE_COMB_LIMITS], limit_rem) 3677 { 3678 struct nlattr *mode; 3679 3680 nla_parse_nested(tb_limit, NUM_NL80211_IFACE_LIMIT, limit, iface_limit_policy); 3681 3682 if (!tb_limit[NL80211_IFACE_LIMIT_TYPES] || 3683 !tb_limit[NL80211_IFACE_LIMIT_MAX]) 3684 continue; 3685 3686 if (nla_get_u32(tb_limit[NL80211_IFACE_LIMIT_MAX]) < 2) 3687 continue; 3688 3689 nla_for_each_nested(mode, tb_limit[NL80211_IFACE_LIMIT_TYPES], mode_rem) { 3690 if (nla_type(mode) == NL80211_IFTYPE_AP) 3691 *ret = 1; 3692 } 3693 } 3694 } 3695 3696 return NL_SKIP; 3697 } 3698 3699 static int nl80211_get_mbssid_support(const char *ifname, int *buf) 3700 { 3701 if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0, 3702 nl80211_get_ifcomb_cb, buf)) 3703 return -1; 3704 3705 return 0; 3706 } 3707 3708 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname) 3709 { 3710 char *phy, path[PATH_MAX]; 3711 3712 /* Try to determine the phy name from the given interface */ 3713 phy = nl80211_ifname2phy(ifname); 3714 3715 snprintf(path, sizeof(path), "/sys/class/%s/%s/device/of_node/compatible", 3716 phy ? "ieee80211" : "net", phy ? phy : ifname); 3717 3718 if (nl80211_readstr(path, id->compatible, sizeof(id->compatible)) <= 0) 3719 return -1; 3720 3721 return 0; 3722 } 3723 3724 3725 static int nl80211_get_hardware_id(const char *ifname, char *buf) 3726 { 3727 struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf; 3728 char *phy, num[8], path[PATH_MAX]; 3729 int i; 3730 3731 struct { const char *path; uint16_t *dest; } lookup[] = { 3732 { "vendor", &id->vendor_id }, 3733 { "device", &id->device_id }, 3734 { "subsystem_vendor", &id->subsystem_vendor_id }, 3735 { "subsystem_device", &id->subsystem_device_id }, 3736 { "../idVendor", &id->subsystem_vendor_id }, 3737 { "../idProduct", &id->subsystem_device_id } 3738 }; 3739 3740 memset(id, 0, sizeof(*id)); 3741 3742 /* Try to determine the phy name from the given interface */ 3743 phy = nl80211_ifname2phy(ifname); 3744 3745 for (i = 0; i < ARRAY_SIZE(lookup); i++) 3746 { 3747 snprintf(path, sizeof(path), "/sys/class/%s/%s/device/%s", 3748 phy ? "ieee80211" : "net", 3749 phy ? phy : ifname, lookup[i].path); 3750 3751 if (nl80211_readstr(path, num, sizeof(num)) > 0) 3752 *lookup[i].dest = strtoul(num, NULL, 16); 3753 } 3754 3755 /* Failed to obtain hardware PCI/USB IDs... */ 3756 if (id->vendor_id == 0 && id->device_id == 0 && 3757 id->subsystem_vendor_id == 0 && id->subsystem_device_id == 0) 3758 /* ... first fallback to FDT ... */ 3759 if (nl80211_hardware_id_from_fdt(id, ifname) == -1) 3760 /* ... then board config */ 3761 return iwinfo_hardware_id_from_mtd(id); 3762 3763 return 0; 3764 } 3765 3766 static const struct iwinfo_hardware_entry * 3767 nl80211_get_hardware_entry(const char *ifname) 3768 { 3769 struct iwinfo_hardware_id id; 3770 3771 if (nl80211_get_hardware_id(ifname, (char *)&id)) 3772 return NULL; 3773 3774 return iwinfo_hardware(&id); 3775 } 3776 3777 static int nl80211_get_hardware_name(const char *ifname, char *buf) 3778 { 3779 const struct iwinfo_hardware_entry *hw; 3780 3781 if (!(hw = nl80211_get_hardware_entry(ifname))) 3782 sprintf(buf, "Generic MAC80211"); 3783 else 3784 sprintf(buf, "%s %s", hw->vendor_name, hw->device_name); 3785 3786 return 0; 3787 } 3788 3789 static int nl80211_get_txpower_offset(const char *ifname, int *buf) 3790 { 3791 const struct iwinfo_hardware_entry *hw; 3792 3793 if (!(hw = nl80211_get_hardware_entry(ifname))) 3794 return -1; 3795 3796 *buf = hw->txpower_offset; 3797 return 0; 3798 } 3799 3800 static int nl80211_get_frequency_offset(const char *ifname, int *buf) 3801 { 3802 const struct iwinfo_hardware_entry *hw; 3803 3804 if (!(hw = nl80211_get_hardware_entry(ifname))) 3805 return -1; 3806 3807 *buf = hw->frequency_offset; 3808 return 0; 3809 } 3810 3811 static int nl80211_lookup_phyname(const char *section, char *buf) 3812 { 3813 const char *name; 3814 int idx; 3815 3816 if (!strncmp(section, "path=", 5)) 3817 idx = nl80211_phy_idx_from_path(section + 5); 3818 else if (!strncmp(section, "macaddr=", 8)) 3819 idx = nl80211_phy_idx_from_macaddr(section + 8); 3820 else 3821 idx = nl80211_phy_idx_from_uci(section); 3822 3823 if (idx < 0) 3824 return -1; 3825 3826 name = nl80211_phyidx2name(idx); 3827 if (!name) 3828 return -1; 3829 3830 strcpy(buf, name); 3831 return 0; 3832 } 3833 3834 static int nl80211_phy_path(const char *phyname, const char **path) 3835 { 3836 if (strchr(phyname, '/')) 3837 return -1; 3838 3839 *path = nl80211_phy_path_str(phyname); 3840 if (!*path) 3841 return -1; 3842 3843 return 0; 3844 } 3845 3846 const struct iwinfo_ops nl80211_ops = { 3847 .name = "nl80211", 3848 .probe = nl80211_probe, 3849 .channel = nl80211_get_channel, 3850 .center_chan1 = nl80211_get_center_chan1, 3851 .center_chan2 = nl80211_get_center_chan2, 3852 .frequency = nl80211_get_frequency, 3853 .frequency_offset = nl80211_get_frequency_offset, 3854 .txpower = nl80211_get_txpower, 3855 .txpower_offset = nl80211_get_txpower_offset, 3856 .bitrate = nl80211_get_bitrate, 3857 .signal = nl80211_get_signal, 3858 .noise = nl80211_get_noise, 3859 .quality = nl80211_get_quality, 3860 .quality_max = nl80211_get_quality_max, 3861 .mbssid_support = nl80211_get_mbssid_support, 3862 .hwmodelist = nl80211_get_hwmodelist, 3863 .htmodelist = nl80211_get_htmodelist, 3864 .htmode = nl80211_get_htmode, 3865 .mode = nl80211_get_mode, 3866 .ssid = nl80211_get_ssid, 3867 .bssid = nl80211_get_bssid, 3868 .country = nl80211_get_country, 3869 .hardware_id = nl80211_get_hardware_id, 3870 .hardware_name = nl80211_get_hardware_name, 3871 .encryption = nl80211_get_encryption, 3872 .phyname = nl80211_get_phyname, 3873 .assoclist = nl80211_get_assoclist, 3874 .txpwrlist = nl80211_get_txpwrlist, 3875 .scanlist = nl80211_get_scanlist, 3876 .freqlist = nl80211_get_freqlist, 3877 .countrylist = nl80211_get_countrylist, 3878 .survey = nl80211_get_survey, 3879 .lookup_phy = nl80211_lookup_phyname, 3880 .phy_path = nl80211_phy_path, 3881 .close = nl80211_close 3882 }; 3883
This page was automatically generated by LXR 0.3.1. • OpenWrt