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