2 * iwinfo - Wireless Information Library - NL80211 Backend
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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/.
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.
22 * Parts of this code are derived from the Linux iw utility.
31 #include "iwinfo_nl80211.h"
33 #define min(x, y) ((x) < (y)) ? (x) : (y)
35 #define BIT(x) (1ULL<<(x))
37 static struct nl80211_state
*nls
= NULL
;
39 static void nl80211_close(void)
44 genl_family_put(nls
->nlctrl
);
47 genl_family_put(nls
->nl80211
);
50 nl_socket_free(nls
->nl_sock
);
53 nl_cache_free(nls
->nl_cache
);
60 static int nl80211_init(void)
66 nls
= malloc(sizeof(struct nl80211_state
));
72 memset(nls
, 0, sizeof(*nls
));
74 nls
->nl_sock
= nl_socket_alloc();
80 if (genl_connect(nls
->nl_sock
)) {
85 fd
= nl_socket_get_fd(nls
->nl_sock
);
86 if (fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
) < 0) {
91 if (genl_ctrl_alloc_cache(nls
->nl_sock
, &nls
->nl_cache
)) {
96 nls
->nl80211
= genl_ctrl_search_by_name(nls
->nl_cache
, "nl80211");
102 nls
->nlctrl
= genl_ctrl_search_by_name(nls
->nl_cache
, "nlctrl");
117 static int nl80211_readint(const char *path
)
123 if ((fd
= open(path
, O_RDONLY
)) > -1)
125 if (read(fd
, buffer
, sizeof(buffer
)) > 0)
134 static int nl80211_readstr(const char *path
, char *buffer
, int length
)
139 if ((fd
= open(path
, O_RDONLY
)) > -1)
141 if ((rv
= read(fd
, buffer
, length
- 1)) > 0)
143 if (buffer
[rv
- 1] == '\n')
156 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
157 struct nlmsgerr
*err
, void *arg
)
164 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
171 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
178 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
183 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
198 static struct nl80211_msg_conveyor
* nl80211_new(struct genl_family
*family
,
201 static struct nl80211_msg_conveyor cv
;
203 struct nl_msg
*req
= NULL
;
204 struct nl_cb
*cb
= NULL
;
210 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
214 genlmsg_put(req
, 0, 0, genl_family_get_id(family
), 0, flags
, cmd
, 0);
228 static struct nl80211_msg_conveyor
* nl80211_ctl(int cmd
, int flags
)
230 if (nl80211_init() < 0)
233 return nl80211_new(nls
->nlctrl
, cmd
, flags
);
236 static int nl80211_phy_idx_from_uci_path(struct uci_section
*s
)
238 size_t linklen
, pathlen
;
239 char buf
[128], *link
;
245 path
= uci_lookup_option_string(uci_ctx
, s
, "path");
249 if ((d
= opendir("/sys/class/ieee80211")) != NULL
)
251 while ((e
= readdir(d
)) != NULL
)
253 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/device", e
->d_name
);
255 link
= realpath(buf
, NULL
);
260 linklen
= strlen(link
);
261 pathlen
= strlen(path
);
263 if (pathlen
>= linklen
|| strcmp(link
+ (linklen
- pathlen
), path
))
271 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
273 idx
= nl80211_readint(buf
);
285 static int nl80211_phy_idx_from_uci_macaddr(struct uci_section
*s
)
292 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
296 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
297 if (glob(buf
, 0, NULL
, &gl
))
300 for (i
= 0; i
< gl
.gl_pathc
; i
++)
302 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
303 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
306 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
309 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
310 if ((idx
= nl80211_readint(buf
)) > -1)
319 static int nl80211_phy_idx_from_uci_phy(struct uci_section
*s
)
324 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
328 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
329 return nl80211_readint(buf
);
332 static int nl80211_phy_idx_from_uci(const char *name
)
334 struct uci_section
*s
;
337 s
= iwinfo_uci_get_radio(name
, "mac80211");
341 idx
= nl80211_phy_idx_from_uci_path(s
);
344 idx
= nl80211_phy_idx_from_uci_macaddr(s
);
347 idx
= nl80211_phy_idx_from_uci_phy(s
);
354 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
357 int ifidx
= -1, phyidx
= -1;
358 struct nl80211_msg_conveyor
*cv
;
363 if (nl80211_init() < 0)
366 if (!strncmp(ifname
, "phy", 3))
367 phyidx
= atoi(&ifname
[3]);
368 else if (!strncmp(ifname
, "radio", 5))
369 phyidx
= nl80211_phy_idx_from_uci(ifname
);
371 if (!strncmp(ifname
, "mon.", 4))
372 ifidx
= if_nametoindex(&ifname
[4]);
374 ifidx
= if_nametoindex(ifname
);
376 /* Valid ifidx must be greater than 0 */
377 if ((ifidx
<= 0) && (phyidx
< 0))
380 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
385 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
386 else if (phyidx
> -1)
387 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
396 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
397 int (*cb_func
)(struct nl_msg
*, void *),
400 static struct nl80211_msg_conveyor rcv
;
404 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
406 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
408 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
415 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
416 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
417 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
420 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
427 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
428 int (*cb_func
)(struct nl_msg
*, void *),
431 struct nl80211_msg_conveyor
*cv
;
433 cv
= nl80211_msg(ifname
, cmd
, flags
);
438 return nl80211_send(cv
, cb_func
, cb_arg
);
441 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
443 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
444 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
446 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
447 genlmsg_attrlen(gnlh
, 0), NULL
);
452 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
454 uint32_t *features
= arg
;
455 struct nlattr
**attr
= nl80211_parse(msg
);
457 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
458 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
463 static int nl80211_get_protocol_features(const char *ifname
)
465 struct nl80211_msg_conveyor
*req
;
466 uint32_t features
= 0;
468 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
470 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
477 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
479 struct nl80211_group_conveyor
*cv
= arg
;
481 struct nlattr
**attr
= nl80211_parse(msg
);
482 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
486 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
489 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
491 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
492 nla_data(mgrp
), nla_len(mgrp
), NULL
);
494 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
495 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
496 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
497 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
499 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
507 static int nl80211_subscribe(const char *family
, const char *group
)
509 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
510 struct nl80211_msg_conveyor
*req
;
513 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
516 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
517 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
522 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
532 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
534 struct nl80211_event_conveyor
*cv
= arg
;
535 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
537 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
538 cv
->recv
= gnlh
->cmd
;
543 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
548 static int __nl80211_wait(const char *family
, const char *group
, ...)
550 struct nl80211_event_conveyor cv
= { };
556 if (nl80211_subscribe(family
, group
))
559 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
564 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
565 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
566 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
570 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
571 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
575 while (!cv
.recv
&& !err
)
576 nl_recvmsgs(nls
->nl_sock
, cb
);
583 #define nl80211_wait(family, group, ...) \
584 __nl80211_wait(family, group, __VA_ARGS__, 0)
587 static int nl80211_freq2channel(int freq
)
591 else if (freq
< 2484)
592 return (freq
- 2407) / 5;
593 else if (freq
>= 4910 && freq
<= 4980)
594 return (freq
- 4000) / 5;
595 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
596 return (freq
- 56160) / 2160;
598 return (freq
- 5000) / 5;
601 static int nl80211_channel2freq(int channel
, const char *band
)
603 if (!band
|| band
[0] != 'a')
607 else if (channel
< 14)
608 return (channel
* 5) + 2407;
610 else if ( strcmp(band
, "ad") == 0)
612 return 56160 + 2160 * channel
;
616 if (channel
>= 182 && channel
<= 196)
617 return (channel
* 5) + 4000;
619 return (channel
* 5) + 5000;
625 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
628 struct nlattr
**attr
= nl80211_parse(msg
);
630 if (attr
[NL80211_ATTR_WIPHY_NAME
])
631 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
632 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
639 static char * nl80211_ifname2phy(const char *ifname
)
641 static char phy
[32] = { 0 };
643 memset(phy
, 0, sizeof(phy
));
645 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
646 nl80211_ifname2phy_cb
, phy
);
648 return phy
[0] ? phy
: NULL
;
651 static char * nl80211_phy2ifname(const char *ifname
)
653 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
655 static char nif
[IFNAMSIZ
] = { 0 };
660 /* Only accept phy name of the form phy%d or radio%d */
663 else if (!strncmp(ifname
, "phy", 3))
664 phyidx
= atoi(&ifname
[3]);
665 else if (!strncmp(ifname
, "radio", 5))
666 phyidx
= nl80211_phy_idx_from_uci(ifname
);
670 memset(nif
, 0, sizeof(nif
));
674 if ((d
= opendir("/sys/class/net")) != NULL
)
676 while ((e
= readdir(d
)) != NULL
)
678 snprintf(buffer
, sizeof(buffer
),
679 "/sys/class/net/%s/phy80211/index", e
->d_name
);
681 if (nl80211_readint(buffer
) == phyidx
)
683 snprintf(buffer
, sizeof(buffer
),
684 "/sys/class/net/%s/ifindex", e
->d_name
);
686 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
687 ((ifidx
< 0) || (cifidx
< ifidx
)))
690 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
699 return nif
[0] ? nif
: NULL
;
702 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
705 struct nlattr
**tb
= nl80211_parse(msg
);
706 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
707 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
708 IWINFO_OPMODE_ADHOC
, /* IBSS */
709 IWINFO_OPMODE_CLIENT
, /* managed */
710 IWINFO_OPMODE_MASTER
, /* AP */
711 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
712 IWINFO_OPMODE_WDS
, /* WDS */
713 IWINFO_OPMODE_MONITOR
, /* monitor */
714 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
715 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
716 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
719 if (tb
[NL80211_ATTR_IFTYPE
])
720 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
726 static int nl80211_get_mode(const char *ifname
, int *buf
)
730 *buf
= IWINFO_OPMODE_UNKNOWN
;
732 res
= nl80211_phy2ifname(ifname
);
734 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
735 nl80211_get_mode_cb
, buf
);
737 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
740 static int __nl80211_hostapd_query(const char *ifname
, ...)
743 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
744 int len
, mode
, found
= 0, match
= 1;
747 if (nl80211_get_mode(ifname
, &mode
))
750 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
753 phy
= nl80211_ifname2phy(ifname
);
758 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
759 fp
= fopen(buf
, "r");
764 va_start(ap
, ifname
);
766 /* clear all destination buffers */
769 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
771 dest
= va_arg(ap_cur
, char *);
772 len
= va_arg(ap_cur
, int);
774 memset(dest
, 0, len
);
779 /* iterate applicable lines and copy found values into dest buffers */
780 while (fgets(buf
, sizeof(buf
), fp
))
782 key
= strtok(buf
, " =\t\n");
783 val
= strtok(NULL
, "\n");
785 if (!key
|| !val
|| !*key
|| *key
== '#')
788 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
789 match
= !strcmp(ifname
, val
);
796 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
798 dest
= va_arg(ap_cur
, char *);
799 len
= va_arg(ap_cur
, int);
801 if (!strcmp(search
, key
))
803 strncpy(dest
, val
, len
- 1);
819 #define nl80211_hostapd_query(ifname, ...) \
820 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
823 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
826 struct timeval tv
= { 0, 256000 };
831 memset(buf
, 0, blen
);
833 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
836 if (!FD_ISSET(sock
, &rfds
))
839 return recv(sock
, buf
, blen
- 1, 0);
842 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
844 struct sockaddr_un remote
= { 0 };
845 size_t remote_length
, local_length
;
847 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
851 remote
.sun_family
= AF_UNIX
;
852 remote_length
= sizeof(remote
.sun_family
) +
853 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
856 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
862 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
864 remote_length
= sizeof(remote
.sun_family
) +
865 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
867 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
874 local
->sun_family
= AF_UNIX
;
875 local_length
= sizeof(local
->sun_family
) +
876 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
878 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
887 static int __nl80211_wpactl_query(const char *ifname
, ...)
890 struct sockaddr_un local
= { 0 };
891 int len
, mode
, found
= 0, sock
= -1;
892 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
894 if (nl80211_get_mode(ifname
, &mode
))
897 if (mode
!= IWINFO_OPMODE_CLIENT
&&
898 mode
!= IWINFO_OPMODE_ADHOC
&&
899 mode
!= IWINFO_OPMODE_MESHPOINT
)
902 sock
= nl80211_wpactl_connect(ifname
, &local
);
907 va_start(ap
, ifname
);
909 /* clear all destination buffers */
912 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
914 dest
= va_arg(ap_cur
, char *);
915 len
= va_arg(ap_cur
, int);
917 memset(dest
, 0, len
);
922 send(sock
, "STATUS", 6, 0);
926 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
932 for (line
= strtok_r(buf
, "\n", &pos
);
934 line
= strtok_r(NULL
, "\n", &pos
))
936 key
= strtok(line
, "=");
937 val
= strtok(NULL
, "\n");
944 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
946 dest
= va_arg(ap_cur
, char *);
947 len
= va_arg(ap_cur
, int);
949 if (!strcmp(search
, key
))
951 strncpy(dest
, val
, len
- 1);
966 unlink(local
.sun_path
);
971 #define nl80211_wpactl_query(ifname, ...) \
972 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
975 static char * nl80211_ifadd(const char *ifname
)
978 static char nif
[IFNAMSIZ
] = { 0 };
979 struct nl80211_msg_conveyor
*req
;
982 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
985 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
987 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
988 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
990 nl80211_send(req
, NULL
, NULL
);
992 snprintf(path
, sizeof(path
) - 1,
993 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
995 if ((sysfs
= fopen(path
, "w")) != NULL
)
997 fwrite("0\n", 1, 2, sysfs
);
1010 static void nl80211_ifdel(const char *ifname
)
1012 struct nl80211_msg_conveyor
*req
;
1015 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1018 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1020 nl80211_send(req
, NULL
, NULL
);
1028 static void nl80211_hostapd_hup(const char *ifname
)
1032 char *phy
= nl80211_ifname2phy(ifname
);
1036 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1037 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1039 if (read(fd
, buf
, sizeof(buf
)) > 0)
1051 static int nl80211_probe(const char *ifname
)
1053 return !!nl80211_ifname2phy(ifname
);
1056 struct nl80211_ssid_bssid
{
1057 unsigned char *ssid
;
1058 unsigned char bssid
[7];
1061 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1063 struct nl80211_ssid_bssid
*sb
= arg
;
1064 struct nlattr
**tb
= nl80211_parse(msg
);
1066 if (tb
[NL80211_ATTR_MAC
]) {
1068 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1069 sizeof(sb
->bssid
) - 1);
1075 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1079 struct nl80211_ssid_bssid
*sb
= arg
;
1080 struct nlattr
**tb
= nl80211_parse(msg
);
1081 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1083 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1084 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1085 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1088 if (!tb
[NL80211_ATTR_BSS
] ||
1089 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1091 !bss
[NL80211_BSS_BSSID
] ||
1092 !bss
[NL80211_BSS_STATUS
] ||
1093 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1098 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1100 case NL80211_BSS_STATUS_ASSOCIATED
:
1101 case NL80211_BSS_STATUS_AUTHENTICATED
:
1102 case NL80211_BSS_STATUS_IBSS_JOINED
:
1106 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1107 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1109 while (ielen
>= 2 && ielen
>= ie
[1])
1113 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1124 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1133 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1136 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1138 /* try to find ssid from scan dump results */
1139 res
= nl80211_phy2ifname(ifname
);
1142 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1143 nl80211_get_ssid_bssid_cb
, &sb
);
1145 /* failed, try to find from hostapd info */
1146 if (sb
.ssid
[0] == 0)
1147 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1148 IWINFO_ESSID_MAX_SIZE
+ 1);
1150 /* failed, try to obtain Mesh ID */
1151 if (sb
.ssid
[0] == 0)
1152 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1153 sb
.ssid
, IWINFO_ESSID_MAX_SIZE
+ 1);
1155 return (sb
.ssid
[0] == 0) ? -1 : 0;
1158 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1160 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1161 struct nl80211_ssid_bssid sb
= { };
1163 res
= nl80211_phy2ifname(ifname
);
1165 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1166 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1167 nl80211_get_macaddr_cb
, &sb
);
1169 /* failed, try to find bssid from scan dump results */
1170 if (sb
.bssid
[0] == 0)
1171 nl80211_request(res
? res
: ifname
,
1172 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1173 nl80211_get_ssid_bssid_cb
, &sb
);
1175 /* failed, try to find mac from hostapd info */
1176 if ((sb
.bssid
[0] == 0) &&
1177 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1180 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1181 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1182 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1183 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1184 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1185 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1190 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1191 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1192 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1201 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1204 struct nlattr
**attr
= nl80211_parse(msg
);
1205 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1207 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1208 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1209 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1212 if (attr
[NL80211_ATTR_BSS
] &&
1213 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1214 attr
[NL80211_ATTR_BSS
], bss_policy
))
1216 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1217 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1223 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1226 struct nlattr
**tb
= nl80211_parse(msg
);
1228 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1229 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1234 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1236 char *res
, channel
[4], hwmode
[3];
1238 /* try to find frequency from interface info */
1239 res
= nl80211_phy2ifname(ifname
);
1242 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1243 nl80211_get_frequency_info_cb
, buf
);
1245 /* failed, try to find frequency from hostapd info */
1247 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1248 "channel", channel
, sizeof(channel
)) == 2)
1250 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1253 /* failed, try to find frequency from scan results */
1256 res
= nl80211_phy2ifname(ifname
);
1258 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1259 nl80211_get_frequency_scan_cb
, buf
);
1262 return (*buf
== 0) ? -1 : 0;
1265 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1268 struct nlattr
**tb
= nl80211_parse(msg
);
1270 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1271 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1276 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1280 /* try to find frequency from interface info */
1281 res
= nl80211_phy2ifname(ifname
);
1284 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1285 nl80211_get_center_freq1_cb
, buf
);
1287 return (*buf
== 0) ? -1 : 0;
1290 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1293 struct nlattr
**tb
= nl80211_parse(msg
);
1295 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1296 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1301 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1305 /* try to find frequency from interface info */
1306 res
= nl80211_phy2ifname(ifname
);
1309 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1310 nl80211_get_center_freq2_cb
, buf
);
1312 return (*buf
== 0) ? -1 : 0;
1315 static int nl80211_get_channel(const char *ifname
, int *buf
)
1317 if (!nl80211_get_frequency(ifname
, buf
))
1319 *buf
= nl80211_freq2channel(*buf
);
1326 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1328 if (!nl80211_get_center_freq1(ifname
, buf
))
1330 *buf
= nl80211_freq2channel(*buf
);
1337 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1339 if (!nl80211_get_center_freq2(ifname
, buf
))
1341 *buf
= nl80211_freq2channel(*buf
);
1348 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1351 struct nlattr
**tb
= nl80211_parse(msg
);
1353 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1354 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1359 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1363 res
= nl80211_phy2ifname(ifname
);
1366 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1367 nl80211_get_txpower_cb
, buf
))
1374 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1378 struct nl80211_rssi_rate
*rr
= arg
;
1379 struct nlattr
**attr
= nl80211_parse(msg
);
1380 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1381 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1383 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1384 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1385 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1386 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1387 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1388 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1389 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1390 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1391 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1392 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1393 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1396 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1397 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1398 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1399 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1400 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1403 if (attr
[NL80211_ATTR_STA_INFO
])
1405 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1406 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1408 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1410 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1411 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1415 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1417 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1418 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1421 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1423 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1424 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1435 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1440 memset(r
, 0, sizeof(*r
));
1442 if ((d
= opendir("/sys/class/net")) != NULL
)
1444 while ((de
= readdir(d
)) != NULL
)
1446 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1447 (!de
->d_name
[strlen(ifname
)] ||
1448 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1450 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1451 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1459 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1461 struct nl80211_rssi_rate rr
;
1463 nl80211_fill_signal(ifname
, &rr
);
1465 if (rr
.rate_samples
)
1467 *buf
= (rr
.rate
* 100);
1474 static int nl80211_get_signal(const char *ifname
, int *buf
)
1476 struct nl80211_rssi_rate rr
;
1478 nl80211_fill_signal(ifname
, &rr
);
1480 if (rr
.rssi_samples
)
1489 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1491 int8_t *noise
= arg
;
1492 struct nlattr
**tb
= nl80211_parse(msg
);
1493 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1495 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1496 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1497 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1500 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1503 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1504 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1507 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1510 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1511 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1517 static int nl80211_get_noise(const char *ifname
, int *buf
)
1521 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1522 nl80211_get_noise_cb
, &noise
))
1533 static int nl80211_get_quality(const char *ifname
, int *buf
)
1537 if (!nl80211_get_signal(ifname
, &signal
))
1539 /* A positive signal level is usually just a quality
1540 * value, pass through as-is */
1546 /* The cfg80211 wext compat layer assumes a signal range
1547 * of -110 dBm to -40 dBm, the quality value is derived
1548 * by adding 110 to the signal level */
1553 else if (signal
> -40)
1556 *buf
= (signal
+ 110);
1565 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1567 /* The cfg80211 wext compat layer assumes a maximum
1574 static int nl80211_check_wepkey(const char *key
)
1578 switch (strlen(key
))
1582 return IWINFO_CIPHER_WEP40
;
1586 return IWINFO_CIPHER_WEP104
;
1597 } wpa_key_mgmt_strings
[] = {
1598 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1599 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1600 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1601 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1602 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1603 { "NONE", 0, IWINFO_KMGMT_NONE
},
1604 { "None", 0, IWINFO_KMGMT_NONE
},
1605 { "PSK", 0, IWINFO_KMGMT_PSK
},
1606 { "EAP", 0, IWINFO_KMGMT_8021x
},
1607 { "SAE", 4, IWINFO_KMGMT_SAE
},
1608 { "OWE", 4, IWINFO_KMGMT_OWE
}
1611 static void parse_wpa_suites(const char *str
, int defversion
,
1612 uint8_t *versions
, uint8_t *suites
)
1616 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1622 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1624 m
= wpa_key_mgmt_strings
[i
].match
;
1627 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1629 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1630 version
= wpa_key_mgmt_strings
[i
].version
;
1632 version
= defversion
;
1634 *versions
|= version
;
1635 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1643 q
+= strcspn(q
, sep
);
1645 p
= q
+ strspn(q
, sep
);
1652 } wpa_cipher_strings
[] = {
1653 { "WEP-104", IWINFO_CIPHER_WEP104
},
1654 { "WEP-40", IWINFO_CIPHER_WEP40
},
1655 { "NONE", IWINFO_CIPHER_NONE
},
1656 { "TKIP", IWINFO_CIPHER_TKIP
},
1657 { "CCMP", IWINFO_CIPHER_CCMP
},
1658 { "GCMP", IWINFO_CIPHER_GCMP
}
1661 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1665 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1671 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1673 m
= wpa_cipher_strings
[i
].match
;
1676 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1678 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1686 q
+= strcspn(q
, sep
);
1688 p
= q
+ strspn(q
, sep
);
1692 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1696 uint8_t wpa_version
= 0;
1697 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1698 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1701 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1703 /* WPA supplicant */
1704 if (nl80211_wpactl_query(ifname
,
1705 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1706 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1707 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1708 "mode", mode
, sizeof(mode
)))
1711 if (!strcmp(wpa_key_mgmt
, "NONE"))
1713 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1714 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1716 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1718 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1719 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1722 c
->pair_ciphers
= 0;
1723 c
->group_ciphers
= 0;
1728 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1732 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1733 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1734 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1740 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1741 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1745 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1750 else if (!strncmp(p
, "WPA-", 4))
1756 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1758 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1765 else if (nl80211_hostapd_query(ifname
,
1766 "wpa", wpa
, sizeof(wpa
),
1767 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1768 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1769 "auth_algs", auth_algs
, sizeof(auth_algs
),
1770 "wep_key0", wep_key0
, sizeof(wep_key0
),
1771 "wep_key1", wep_key1
, sizeof(wep_key1
),
1772 "wep_key2", wep_key2
, sizeof(wep_key2
),
1773 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1777 if (wpa_key_mgmt
[0])
1779 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1781 if (!strncmp(p
, "WPA-", 4))
1784 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1787 c
->enabled
= c
->wpa_version
? 1 : 0;
1790 if (wpa_pairwise
[0])
1791 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1795 switch (atoi(auth_algs
))
1798 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1802 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1806 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1807 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1811 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1812 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1813 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1814 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1816 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1819 c
->group_ciphers
= c
->pair_ciphers
;
1824 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1825 else if (!nl80211_get_mode(ifname
, &opmode
) &&
1826 (opmode
== IWINFO_OPMODE_ADHOC
||
1827 opmode
== IWINFO_OPMODE_MESHPOINT
))
1838 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1842 name
= nl80211_ifname2phy(ifname
);
1849 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1851 name
= nl80211_ifname2phy(name
);
1855 strcpy(buf
, ifname
);
1864 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1865 struct iwinfo_rate_entry
*re
)
1867 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1868 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1869 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1870 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1872 if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1875 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1877 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1878 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1880 else if (ri
[NL80211_RATE_INFO_MCS
])
1883 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1886 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1888 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1890 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1892 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1894 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1895 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
1900 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
1901 re
->is_short_gi
= 1;
1903 re
->is_40mhz
= (re
->mhz
== 40);
1906 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
1908 struct nl80211_array_buf
*arr
= arg
;
1909 struct iwinfo_survey_entry
*e
= arr
->buf
;
1910 struct nlattr
**attr
= nl80211_parse(msg
);
1911 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
1914 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1915 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1916 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1917 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
1918 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
1919 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
1920 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
1921 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
1924 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
1925 attr
[NL80211_ATTR_SURVEY_INFO
],
1930 /* advance to end of array */
1932 memset(e
, 0, sizeof(*e
));
1934 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
1935 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
1937 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
1938 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
1940 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
1941 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
1943 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
1944 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
1946 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
1947 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
1949 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
1950 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
1952 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
1953 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
1960 static void plink_state_to_str(char *dst
, unsigned state
)
1963 case NL80211_PLINK_LISTEN
:
1964 strcpy(dst
, "LISTEN");
1966 case NL80211_PLINK_OPN_SNT
:
1967 strcpy(dst
, "OPN_SNT");
1969 case NL80211_PLINK_OPN_RCVD
:
1970 strcpy(dst
, "OPN_RCVD");
1972 case NL80211_PLINK_CNF_RCVD
:
1973 strcpy(dst
, "CNF_RCVD");
1975 case NL80211_PLINK_ESTAB
:
1976 strcpy(dst
, "ESTAB");
1978 case NL80211_PLINK_HOLDING
:
1979 strcpy(dst
, "HOLDING");
1981 case NL80211_PLINK_BLOCKED
:
1982 strcpy(dst
, "BLOCKED");
1985 strcpy(dst
, "UNKNOWN");
1990 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
1992 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
1995 case NL80211_MESH_POWER_ACTIVE
:
1996 strcpy(dst
, "ACTIVE");
1998 case NL80211_MESH_POWER_LIGHT_SLEEP
:
1999 strcpy(dst
, "LIGHT SLEEP");
2001 case NL80211_MESH_POWER_DEEP_SLEEP
:
2002 strcpy(dst
, "DEEP SLEEP");
2005 strcpy(dst
, "UNKNOWN");
2010 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2012 struct nl80211_array_buf
*arr
= arg
;
2013 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2014 struct nlattr
**attr
= nl80211_parse(msg
);
2015 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2016 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2017 struct nl80211_sta_flag_update
*sta_flags
;
2019 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2020 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2021 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2022 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2023 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2024 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2025 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2026 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2027 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2028 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2029 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2030 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2031 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2032 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2033 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2034 [NL80211_STA_INFO_STA_FLAGS
] =
2035 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2036 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2038 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2039 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2040 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2041 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2042 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2043 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2046 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2047 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2048 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2049 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2050 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2053 /* advance to end of array */
2055 memset(e
, 0, sizeof(*e
));
2057 if (attr
[NL80211_ATTR_MAC
])
2058 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2060 if (attr
[NL80211_ATTR_STA_INFO
] &&
2061 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2062 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2064 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2065 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2067 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2068 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2070 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2071 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2073 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2074 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2076 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2077 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2079 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2080 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2082 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2083 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2084 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2085 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2087 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2088 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2089 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2090 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2092 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2093 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2095 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2096 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2098 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2099 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2101 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2102 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2104 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2105 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2107 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2108 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2110 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2111 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2114 if (sinfo
[NL80211_STA_INFO_LLID
])
2115 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2117 if (sinfo
[NL80211_STA_INFO_PLID
])
2118 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2120 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2121 plink_state_to_str(e
->plink_state
,
2122 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2124 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2125 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2126 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2127 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2128 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2129 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2132 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2134 sta_flags
= (struct nl80211_sta_flag_update
*)
2135 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2137 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2138 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2139 e
->is_authorized
= 1;
2141 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2142 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2143 e
->is_authenticated
= 1;
2145 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2146 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2147 e
->is_preamble_short
= 1;
2149 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2150 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2153 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2154 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2157 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2158 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2163 e
->noise
= 0; /* filled in by caller */
2169 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2171 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2174 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2175 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2177 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2184 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2189 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2190 struct iwinfo_assoclist_entry
*e
;
2192 if ((d
= opendir("/sys/class/net")) != NULL
)
2194 while ((de
= readdir(d
)) != NULL
)
2196 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2197 (!de
->d_name
[strlen(ifname
)] ||
2198 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2200 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2201 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2207 if (!nl80211_get_noise(ifname
, &noise
))
2208 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2211 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2218 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2221 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2223 struct nlattr
**attr
= nl80211_parse(msg
);
2224 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2225 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2226 struct nlattr
*band
, *freq
;
2228 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2229 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2230 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2231 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2232 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2233 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2234 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2237 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2240 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2242 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2243 nla_len(band
), NULL
);
2245 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2247 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2248 nla_data(freq
), nla_len(freq
), freq_policy
);
2250 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2251 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2253 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2254 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2256 *dbm_max
= (int)(0.01 * nla_get_u32(
2257 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2267 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2270 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2271 struct nl80211_msg_conveyor
*req
;
2272 struct iwinfo_txpwrlist_entry entry
;
2274 if (nl80211_get_channel(ifname
, &ch_cur
))
2277 /* initialize the value pointer with channel for callback */
2280 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2281 nl80211_get_txpwrlist_cb
, &dbm_max
);
2285 for (dbm_cur
= 0, dbm_cnt
= 0;
2287 dbm_cur
++, dbm_cnt
++)
2289 entry
.dbm
= dbm_cur
;
2290 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2292 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2295 entry
.dbm
= dbm_max
;
2296 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2298 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2301 *len
= dbm_cnt
* sizeof(entry
);
2308 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2310 int wpa_version
= 0;
2311 char *p
, *q
, *proto
, *suites
;
2315 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2316 if (!strcmp(p
, "WEP")) {
2318 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2319 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2320 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2324 proto
= strtok(p
, "-");
2325 suites
= strtok(NULL
, "]");
2327 if (!proto
|| !suites
)
2330 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2332 else if (!strcmp(proto
, "WPA"))
2339 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2340 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2345 struct nl80211_scanlist
{
2346 struct iwinfo_scanlist_entry
*e
;
2351 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2352 struct iwinfo_scanlist_entry
*e
)
2354 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2355 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2356 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2359 while (ielen
>= 2 && ielen
>= ie
[1])
2364 case 114: /* Mesh ID */
2365 if (e
->ssid
[0] == 0) {
2366 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2367 memcpy(e
->ssid
, ie
+ 2, len
);
2373 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2374 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2377 case 221: /* Vendor */
2378 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2379 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2380 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2382 case 61: /* HT oeration */
2384 e
->ht_chan_info
.primary_chan
= ie
[2];
2385 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2386 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2389 case 192: /* VHT operation */
2391 e
->vht_chan_info
.chan_width
= ie
[2];
2392 e
->vht_chan_info
.center_chan_1
= ie
[3];
2393 e
->vht_chan_info
.center_chan_2
= ie
[4];
2403 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2408 struct nl80211_scanlist
*sl
= arg
;
2409 struct nlattr
**tb
= nl80211_parse(msg
);
2410 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2412 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2413 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2414 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2415 [NL80211_BSS_BSSID
] = { 0 },
2416 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2417 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2418 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2419 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2420 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2421 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2422 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2423 [NL80211_BSS_BEACON_IES
] = { 0 },
2426 if (!tb
[NL80211_ATTR_BSS
] ||
2427 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2429 !bss
[NL80211_BSS_BSSID
])
2434 if (bss
[NL80211_BSS_CAPABILITY
])
2435 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2439 memset(sl
->e
, 0, sizeof(*sl
->e
));
2440 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2443 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2444 else if (caps
& (1<<0))
2445 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2447 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2450 sl
->e
->crypto
.enabled
= 1;
2452 if (bss
[NL80211_BSS_FREQUENCY
])
2453 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2454 bss
[NL80211_BSS_FREQUENCY
]));
2456 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2457 nl80211_get_scanlist_ie(bss
, sl
->e
);
2459 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2462 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2464 rssi
= sl
->e
->signal
- 0x100;
2468 else if (rssi
> -40)
2471 sl
->e
->quality
= (rssi
+ 110);
2472 sl
->e
->quality_max
= 70;
2475 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2477 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2478 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2487 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2489 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2491 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2494 if (nl80211_wait("nl80211", "scan",
2495 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2498 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2499 nl80211_get_scanlist_cb
, &sl
))
2502 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2510 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2513 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2514 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2520 if (len
+ 1 >= outlen
)
2530 out
[len
++] = '\n'; in
++;
2534 out
[len
++] = '\r'; in
++;
2538 out
[len
++] = '\t'; in
++;
2542 out
[len
++] = '\033'; in
++;
2546 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2547 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2569 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2571 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2572 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2573 struct sockaddr_un local
= { 0 };
2574 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2576 sock
= nl80211_wpactl_connect(ifname
, &local
);
2581 send(sock
, "ATTACH", 6, 0);
2582 send(sock
, "SCAN", 4, 0);
2585 * wait for scan results:
2586 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2587 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2588 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2589 * time to process the results, so try 72 + 2 + 1 times.
2591 for (tries
= 0; tries
< 75; tries
++)
2593 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2596 /* got an event notification */
2597 if (reply
[0] == '<')
2599 /* scan results are ready */
2600 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2602 /* send "SCAN_RESULTS" command */
2603 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2607 /* is another unrelated event, retry */
2611 /* scanning already in progress, keep awaiting results */
2612 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2617 /* another failure, abort */
2618 else if (!strncmp(reply
, "FAIL-", 5))
2624 /* receive and parse scan results if the wait above didn't time out */
2625 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2627 /* received an event notification, receive again */
2628 if (reply
[0] == '<')
2631 nl80211_get_quality_max(ifname
, &qmax
);
2633 for (line
= strtok_r(reply
, "\n", &pos
);
2635 line
= strtok_r(NULL
, "\n", &pos
))
2637 /* skip header line */
2644 bssid
= strtok(line
, "\t");
2645 freq
= strtok(NULL
, "\t");
2646 signal
= strtok(NULL
, "\t");
2647 flags
= strtok(NULL
, "\t");
2648 ssid
= strtok(NULL
, "\n");
2650 if (!bssid
|| !freq
|| !signal
|| !flags
)
2654 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2655 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2656 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2657 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2658 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2659 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2663 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2668 if (strstr(flags
, "[MESH]"))
2669 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2670 else if (strstr(flags
, "[IBSS]"))
2671 e
->mode
= IWINFO_OPMODE_ADHOC
;
2673 e
->mode
= IWINFO_OPMODE_MASTER
;
2676 e
->channel
= nl80211_freq2channel(atoi(freq
));
2679 rssi
= atoi(signal
);
2685 /* The cfg80211 wext compat layer assumes a signal range
2686 * of -110 dBm to -40 dBm, the quality value is derived
2687 * by adding 110 to the signal level */
2690 else if (rssi
> -40)
2693 e
->quality
= (rssi
+ 110);
2701 e
->quality_max
= qmax
;
2704 nl80211_get_scancrypto(flags
, &e
->crypto
);
2710 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2715 unlink(local
.sun_path
);
2717 return (count
>= 0) ? 0 : -1;
2720 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2727 /* Got a radioX pseudo interface, find some interface on it or create one */
2728 if (!strncmp(ifname
, "radio", 5))
2730 /* Reuse existing interface */
2731 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2733 return nl80211_get_scanlist(res
, buf
, len
);
2736 /* Need to spawn a temporary iface for scanning */
2737 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2739 rv
= nl80211_get_scanlist(res
, buf
, len
);
2745 /* WPA supplicant */
2746 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2751 /* station / ad-hoc / monitor scan */
2752 else if (!nl80211_get_mode(ifname
, &mode
) &&
2753 (mode
== IWINFO_OPMODE_ADHOC
||
2754 mode
== IWINFO_OPMODE_MASTER
||
2755 mode
== IWINFO_OPMODE_CLIENT
||
2756 mode
== IWINFO_OPMODE_MONITOR
) &&
2757 iwinfo_ifup(ifname
))
2759 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2765 /* Got a temp interface, don't create yet another one */
2766 if (!strncmp(ifname
, "tmp.", 4))
2768 if (!iwinfo_ifup(ifname
))
2771 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2772 iwinfo_ifdown(ifname
);
2776 /* Spawn a new scan interface */
2779 if (!(res
= nl80211_ifadd(ifname
)))
2784 /* if we can take the new interface up, the driver supports an
2785 * additional interface and there's no need to tear down the ap */
2786 if (iwinfo_ifup(res
))
2788 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2792 /* driver cannot create secondary interface, take down ap
2794 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2796 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2798 iwinfo_ifup(ifname
);
2799 nl80211_hostapd_hup(ifname
);
2810 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2812 int bands_remain
, freqs_remain
;
2814 struct nl80211_array_buf
*arr
= arg
;
2815 struct iwinfo_freqlist_entry
*e
;
2817 struct nlattr
**attr
= nl80211_parse(msg
);
2818 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2819 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2820 struct nlattr
*band
, *freq
;
2825 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2826 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2828 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2829 nla_data(band
), nla_len(band
), NULL
);
2831 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2832 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2834 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2835 nla_data(freq
), nla_len(freq
), NULL
);
2837 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2838 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2841 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2842 e
->channel
= nl80211_freq2channel(e
->mhz
);
2845 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2846 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2849 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2850 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2851 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2852 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2853 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2854 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2855 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2856 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2857 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2858 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2859 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2860 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2872 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2874 struct nl80211_msg_conveyor
*cv
;
2875 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2876 uint32_t features
= nl80211_get_protocol_features(ifname
);
2879 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
2880 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
2884 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
2885 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
2888 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
2898 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
2901 struct nlattr
**attr
= nl80211_parse(msg
);
2903 if (attr
[NL80211_ATTR_REG_ALPHA2
])
2904 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
2911 static int nl80211_get_country(const char *ifname
, char *buf
)
2913 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
2914 nl80211_get_country_cb
, buf
))
2920 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
2923 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
2924 const struct iwinfo_iso3166_label
*l
;
2926 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
2928 e
->iso3166
= l
->iso3166
;
2929 e
->ccode
[0] = (l
->iso3166
/ 256);
2930 e
->ccode
[1] = (l
->iso3166
% 256);
2934 *len
= (count
* sizeof(struct iwinfo_country_entry
));
2939 struct nl80211_modes
2946 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
2948 struct nl80211_modes
*m
= arg
;
2949 int bands_remain
, freqs_remain
;
2951 uint32_t vht_caps
= 0;
2952 struct nlattr
**attr
= nl80211_parse(msg
);
2953 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2954 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2955 struct nlattr
*band
, *freq
;
2957 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
2959 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2961 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2962 nla_data(band
), nla_len(band
), NULL
);
2964 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
2965 caps
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
2967 /* Treat any nonzero capability as 11n */
2970 m
->hw
|= IWINFO_80211_N
;
2971 m
->ht
|= IWINFO_HTMODE_HT20
;
2973 if (caps
& (1 << 1))
2974 m
->ht
|= IWINFO_HTMODE_HT40
;
2977 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
2980 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2981 nla_data(freq
), nla_len(freq
), NULL
);
2983 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
2986 if (nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]) < 2485)
2988 m
->hw
|= IWINFO_80211_B
;
2989 m
->hw
|= IWINFO_80211_G
;
2991 else if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
2993 vht_caps
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
2995 /* Treat any nonzero capability as 11ac */
2998 m
->hw
|= IWINFO_80211_AC
;
2999 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3001 switch ((vht_caps
>> 2) & 3)
3004 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3008 m
->ht
|= IWINFO_HTMODE_VHT160
;
3012 else if (nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]) >= 56160)
3014 m
->hw
|= IWINFO_80211_AD
;
3016 else if (!(m
->hw
& IWINFO_80211_AC
))
3018 m
->hw
|= IWINFO_80211_A
;
3029 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3031 struct nl80211_modes m
= { 0 };
3033 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3034 nl80211_get_modelist_cb
, &m
))
3053 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3055 struct nlattr
**tb
= nl80211_parse(msg
);
3057 struct chan_info
*chn
= arg
;
3059 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3060 chn
->width
= nla_get_u32(cur
);
3062 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3063 chn
->mode
= nla_get_u32(cur
);
3068 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3070 struct chan_info chn
= { .width
= 0, .mode
= 0 };
3074 res
= nl80211_phy2ifname(ifname
);
3077 err
= nl80211_request(res
? res
: ifname
,
3078 NL80211_CMD_GET_INTERFACE
, 0,
3079 nl80211_get_htmode_cb
, &chn
);
3083 switch (chn
.width
) {
3084 case NL80211_CHAN_WIDTH_20
:
3086 *buf
= IWINFO_HTMODE_VHT20
;
3088 *buf
= IWINFO_HTMODE_HT20
;
3090 case NL80211_CHAN_WIDTH_40
:
3092 *buf
= IWINFO_HTMODE_VHT40
;
3094 *buf
= IWINFO_HTMODE_HT40
;
3096 case NL80211_CHAN_WIDTH_80
:
3097 *buf
= IWINFO_HTMODE_VHT80
;
3099 case NL80211_CHAN_WIDTH_80P80
:
3100 *buf
= IWINFO_HTMODE_VHT80_80
;
3102 case NL80211_CHAN_WIDTH_160
:
3103 *buf
= IWINFO_HTMODE_VHT160
;
3105 case NL80211_CHAN_WIDTH_5
:
3106 case NL80211_CHAN_WIDTH_10
:
3107 case NL80211_CHAN_WIDTH_20_NOHT
:
3108 *buf
= IWINFO_HTMODE_NOHT
;
3117 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3119 struct nl80211_modes m
= { 0 };
3121 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3122 nl80211_get_modelist_cb
, &m
))
3137 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3139 struct nlattr
**attr
= nl80211_parse(msg
);
3140 struct nlattr
*comb
;
3142 int comb_rem
, limit_rem
, mode_rem
;
3145 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3148 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3150 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3151 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3152 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3154 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3155 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3156 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3157 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3159 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3160 struct nlattr
*limit
;
3162 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3164 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3167 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3169 struct nlattr
*mode
;
3171 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3173 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3174 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3177 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3180 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3181 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3190 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3192 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3193 nl80211_get_ifcomb_cb
, buf
))
3199 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3201 char *phy
, compat
[64], path
[PATH_MAX
];
3204 /* Try to determine the phy name from the given interface */
3205 phy
= nl80211_ifname2phy(ifname
);
3207 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3208 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3210 if (nl80211_readstr(path
, compat
, sizeof(compat
)) <= 0)
3213 if (!strcmp(compat
, "qca,ar9130-wmac")) {
3214 id
->vendor_id
= 0x168c;
3215 id
->device_id
= 0x0029;
3216 id
->subsystem_vendor_id
= 0x168c;
3217 id
->subsystem_device_id
= 0x9130;
3218 } else if (!strcmp(compat
, "qca,ar9330-wmac")) {
3219 id
->vendor_id
= 0x168c;
3220 id
->device_id
= 0x0030;
3221 id
->subsystem_vendor_id
= 0x168c;
3222 id
->subsystem_device_id
= 0x9330;
3223 } else if (!strcmp(compat
, "qca,ar9340-wmac")) {
3224 id
->vendor_id
= 0x168c;
3225 id
->device_id
= 0x0030;
3226 id
->subsystem_vendor_id
= 0x168c;
3227 id
->subsystem_device_id
= 0x9340;
3228 } else if (!strcmp(compat
, "qca,qca9530-wmac")) {
3229 id
->vendor_id
= 0x168c;
3230 id
->device_id
= 0x0033;
3231 id
->subsystem_vendor_id
= 0x168c;
3232 id
->subsystem_device_id
= 0x9530;
3233 } else if (!strcmp(compat
, "qca,qca9550-wmac")) {
3234 id
->vendor_id
= 0x168c;
3235 id
->device_id
= 0x0033;
3236 id
->subsystem_vendor_id
= 0x168c;
3237 id
->subsystem_device_id
= 0x9550;
3238 } else if (!strcmp(compat
, "qca,qca9560-wmac")) {
3239 id
->vendor_id
= 0x168c;
3240 id
->device_id
= 0x0033;
3241 id
->subsystem_vendor_id
= 0x168c;
3242 id
->subsystem_device_id
= 0x9560;
3243 } else if (!strcmp(compat
, "qcom,ipq4019-wifi")) {
3244 id
->vendor_id
= 0x168c;
3245 id
->device_id
= 0x003c;
3246 id
->subsystem_vendor_id
= 0x168c;
3247 id
->subsystem_device_id
= 0x4019;
3248 } else if (!strcmp(compat
, "mediatek,mt7622-wmac")) {
3249 id
->vendor_id
= 0x14c3;
3250 id
->device_id
= 0x7622;
3251 id
->subsystem_vendor_id
= 0x14c3;
3252 id
->subsystem_device_id
= 0x7622;
3254 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
3258 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3260 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3261 char *phy
, num
[8], path
[PATH_MAX
];
3264 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3265 { "vendor", &id
->vendor_id
},
3266 { "device", &id
->device_id
},
3267 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3268 { "subsystem_device", &id
->subsystem_device_id
}
3271 memset(id
, 0, sizeof(*id
));
3273 /* Try to determine the phy name from the given interface */
3274 phy
= nl80211_ifname2phy(ifname
);
3276 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3278 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3279 phy
? "ieee80211" : "net",
3280 phy
? phy
: ifname
, lookup
[i
].path
);
3282 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3283 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3286 /* Failed to obtain hardware IDs, try FDT */
3287 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3288 if (!nl80211_hardware_id_from_fdt(id
, ifname
))
3291 /* Failed to obtain hardware IDs, search board config */
3292 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3293 return iwinfo_hardware_id_from_mtd(id
);
3298 static const struct iwinfo_hardware_entry
*
3299 nl80211_get_hardware_entry(const char *ifname
)
3301 struct iwinfo_hardware_id id
;
3303 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3306 return iwinfo_hardware(&id
);
3309 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3311 const struct iwinfo_hardware_entry
*hw
;
3313 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3314 sprintf(buf
, "Generic MAC80211");
3316 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3321 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3323 const struct iwinfo_hardware_entry
*hw
;
3325 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3328 *buf
= hw
->txpower_offset
;
3332 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3334 const struct iwinfo_hardware_entry
*hw
;
3336 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3339 *buf
= hw
->frequency_offset
;
3343 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3347 if ((idx
= nl80211_phy_idx_from_uci(section
)) < 0)
3350 sprintf(buf
, "phy%d", idx
);
3354 const struct iwinfo_ops nl80211_ops
= {
3356 .probe
= nl80211_probe
,
3357 .channel
= nl80211_get_channel
,
3358 .center_chan1
= nl80211_get_center_chan1
,
3359 .center_chan2
= nl80211_get_center_chan2
,
3360 .frequency
= nl80211_get_frequency
,
3361 .frequency_offset
= nl80211_get_frequency_offset
,
3362 .txpower
= nl80211_get_txpower
,
3363 .txpower_offset
= nl80211_get_txpower_offset
,
3364 .bitrate
= nl80211_get_bitrate
,
3365 .signal
= nl80211_get_signal
,
3366 .noise
= nl80211_get_noise
,
3367 .quality
= nl80211_get_quality
,
3368 .quality_max
= nl80211_get_quality_max
,
3369 .mbssid_support
= nl80211_get_mbssid_support
,
3370 .hwmodelist
= nl80211_get_hwmodelist
,
3371 .htmodelist
= nl80211_get_htmodelist
,
3372 .htmode
= nl80211_get_htmode
,
3373 .mode
= nl80211_get_mode
,
3374 .ssid
= nl80211_get_ssid
,
3375 .bssid
= nl80211_get_bssid
,
3376 .country
= nl80211_get_country
,
3377 .hardware_id
= nl80211_get_hardware_id
,
3378 .hardware_name
= nl80211_get_hardware_name
,
3379 .encryption
= nl80211_get_encryption
,
3380 .phyname
= nl80211_get_phyname
,
3381 .assoclist
= nl80211_get_assoclist
,
3382 .txpwrlist
= nl80211_get_txpwrlist
,
3383 .scanlist
= nl80211_get_scanlist
,
3384 .freqlist
= nl80211_get_freqlist
,
3385 .countrylist
= nl80211_get_countrylist
,
3386 .survey
= nl80211_get_survey
,
3387 .lookup_phy
= nl80211_lookup_phyname
,
3388 .close
= nl80211_close