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 const char *nl80211_phy_path_str(const char *phyname
)
238 static char path
[PATH_MAX
];
239 const char *prefix
= "/sys/devices/";
240 int prefix_len
= strlen(prefix
);
243 char buf
[128], *link
;
248 phy_id
= atoi(phyname
+ 3);
249 buf_len
= snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/device", phyname
);
250 link
= realpath(buf
, path
);
254 if (strncmp(link
, prefix
, prefix_len
) != 0)
259 prefix
= "platform/";
260 prefix_len
= strlen(prefix
);
261 if (!strncmp(link
, prefix
, prefix_len
) && strstr(link
, "/pci"))
264 snprintf(buf
+ buf_len
, sizeof(buf
) - buf_len
, "/ieee80211");
269 while ((e
= readdir(d
)) != NULL
) {
272 if (strncmp(e
->d_name
, "phy", 3) != 0)
275 cur_id
= atoi(e
->d_name
+ 3);
276 if (cur_id
>= phy_id
)
287 offset
= link
- path
+ strlen(link
);
288 snprintf(path
+ offset
, sizeof(path
) - offset
, "+%d", seq
);
293 static int nl80211_phy_idx_from_path(const char *path
)
297 const char *cur_path
;
306 path_len
= strlen(path
);
310 d
= opendir("/sys/class/ieee80211");
314 while ((e
= readdir(d
)) != NULL
) {
315 cur_path
= nl80211_phy_path_str(e
->d_name
);
319 cur_path_len
= strlen(cur_path
);
320 if (cur_path_len
< path_len
)
323 if (strcmp(cur_path
+ cur_path_len
- path_len
, path
) != 0)
326 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
327 idx
= nl80211_readint(buf
);
338 static int nl80211_phy_idx_from_macaddr(const char *opt
)
347 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
348 if (glob(buf
, 0, NULL
, &gl
))
351 for (i
= 0; i
< gl
.gl_pathc
; i
++)
353 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
354 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
357 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
360 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
361 if ((idx
= nl80211_readint(buf
)) > -1)
370 static int nl80211_phy_idx_from_phy(const char *opt
)
377 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
378 return nl80211_readint(buf
);
381 static int nl80211_phy_idx_from_uci(const char *name
)
383 struct uci_section
*s
;
387 s
= iwinfo_uci_get_radio(name
, "mac80211");
391 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
392 idx
= nl80211_phy_idx_from_path(opt
);
396 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
397 idx
= nl80211_phy_idx_from_macaddr(opt
);
401 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
402 idx
= nl80211_phy_idx_from_phy(opt
);
409 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
412 int ifidx
= -1, phyidx
= -1;
413 struct nl80211_msg_conveyor
*cv
;
418 if (nl80211_init() < 0)
421 if (!strncmp(ifname
, "phy", 3))
422 phyidx
= atoi(&ifname
[3]);
423 else if (!strncmp(ifname
, "radio", 5))
424 phyidx
= nl80211_phy_idx_from_uci(ifname
);
426 if (!strncmp(ifname
, "mon.", 4))
427 ifidx
= if_nametoindex(&ifname
[4]);
429 ifidx
= if_nametoindex(ifname
);
431 /* Valid ifidx must be greater than 0 */
432 if ((ifidx
<= 0) && (phyidx
< 0))
435 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
440 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
441 else if (phyidx
> -1)
442 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
451 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
452 int (*cb_func
)(struct nl_msg
*, void *),
455 static struct nl80211_msg_conveyor rcv
;
459 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
461 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
463 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
470 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
471 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
472 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
475 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
482 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
483 int (*cb_func
)(struct nl_msg
*, void *),
486 struct nl80211_msg_conveyor
*cv
;
488 cv
= nl80211_msg(ifname
, cmd
, flags
);
493 return nl80211_send(cv
, cb_func
, cb_arg
);
496 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
498 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
499 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
501 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
502 genlmsg_attrlen(gnlh
, 0), NULL
);
507 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
509 uint32_t *features
= arg
;
510 struct nlattr
**attr
= nl80211_parse(msg
);
512 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
513 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
518 static int nl80211_get_protocol_features(const char *ifname
)
520 struct nl80211_msg_conveyor
*req
;
521 uint32_t features
= 0;
523 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
525 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
532 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
534 struct nl80211_group_conveyor
*cv
= arg
;
536 struct nlattr
**attr
= nl80211_parse(msg
);
537 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
541 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
544 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
546 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
547 nla_data(mgrp
), nla_len(mgrp
), NULL
);
549 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
550 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
551 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
552 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
554 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
562 static int nl80211_subscribe(const char *family
, const char *group
)
564 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
565 struct nl80211_msg_conveyor
*req
;
568 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
571 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
572 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
577 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
587 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
589 struct nl80211_event_conveyor
*cv
= arg
;
590 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
592 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
593 cv
->recv
= gnlh
->cmd
;
598 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
603 static int __nl80211_wait(const char *family
, const char *group
, ...)
605 struct nl80211_event_conveyor cv
= { };
611 if (nl80211_subscribe(family
, group
))
614 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
619 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
620 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
621 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
625 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
626 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
630 while (!cv
.recv
&& !err
)
631 nl_recvmsgs(nls
->nl_sock
, cb
);
638 #define nl80211_wait(family, group, ...) \
639 __nl80211_wait(family, group, __VA_ARGS__, 0)
642 static int nl80211_freq2channel(int freq
)
646 else if (freq
< 2484)
647 return (freq
- 2407) / 5;
648 else if (freq
>= 4910 && freq
<= 4980)
649 return (freq
- 4000) / 5;
650 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
651 return (freq
- 56160) / 2160;
653 return (freq
- 5000) / 5;
656 static int nl80211_channel2freq(int channel
, const char *band
)
658 if (!band
|| band
[0] != 'a')
662 else if (channel
< 14)
663 return (channel
* 5) + 2407;
665 else if ( strcmp(band
, "ad") == 0)
667 return 56160 + 2160 * channel
;
671 if (channel
>= 182 && channel
<= 196)
672 return (channel
* 5) + 4000;
674 return (channel
* 5) + 5000;
680 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
683 struct nlattr
**attr
= nl80211_parse(msg
);
685 if (attr
[NL80211_ATTR_WIPHY_NAME
])
686 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
687 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
694 static char * nl80211_ifname2phy(const char *ifname
)
696 static char phy
[32] = { 0 };
698 memset(phy
, 0, sizeof(phy
));
700 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
701 nl80211_ifname2phy_cb
, phy
);
703 return phy
[0] ? phy
: NULL
;
706 static char * nl80211_phy2ifname(const char *ifname
)
708 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
710 static char nif
[IFNAMSIZ
] = { 0 };
715 /* Only accept phy name of the form phy%d or radio%d */
718 else if (!strncmp(ifname
, "phy", 3))
719 phyidx
= atoi(&ifname
[3]);
720 else if (!strncmp(ifname
, "radio", 5))
721 phyidx
= nl80211_phy_idx_from_uci(ifname
);
725 memset(nif
, 0, sizeof(nif
));
729 if ((d
= opendir("/sys/class/net")) != NULL
)
731 while ((e
= readdir(d
)) != NULL
)
733 snprintf(buffer
, sizeof(buffer
),
734 "/sys/class/net/%s/phy80211/index", e
->d_name
);
736 if (nl80211_readint(buffer
) == phyidx
)
738 snprintf(buffer
, sizeof(buffer
),
739 "/sys/class/net/%s/ifindex", e
->d_name
);
741 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
742 ((ifidx
< 0) || (cifidx
< ifidx
)))
745 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
754 return nif
[0] ? nif
: NULL
;
757 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
760 struct nlattr
**tb
= nl80211_parse(msg
);
761 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
762 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
763 IWINFO_OPMODE_ADHOC
, /* IBSS */
764 IWINFO_OPMODE_CLIENT
, /* managed */
765 IWINFO_OPMODE_MASTER
, /* AP */
766 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
767 IWINFO_OPMODE_WDS
, /* WDS */
768 IWINFO_OPMODE_MONITOR
, /* monitor */
769 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
770 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
771 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
774 if (tb
[NL80211_ATTR_IFTYPE
])
775 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
781 static int nl80211_get_mode(const char *ifname
, int *buf
)
785 *buf
= IWINFO_OPMODE_UNKNOWN
;
787 res
= nl80211_phy2ifname(ifname
);
789 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
790 nl80211_get_mode_cb
, buf
);
792 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
795 static int __nl80211_hostapd_query(const char *ifname
, ...)
798 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
799 int len
, mode
, found
= 0, match
= 1;
802 if (nl80211_get_mode(ifname
, &mode
))
805 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
808 phy
= nl80211_ifname2phy(ifname
);
813 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
814 fp
= fopen(buf
, "r");
819 va_start(ap
, ifname
);
821 /* clear all destination buffers */
824 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
826 dest
= va_arg(ap_cur
, char *);
827 len
= va_arg(ap_cur
, int);
829 memset(dest
, 0, len
);
834 /* iterate applicable lines and copy found values into dest buffers */
835 while (fgets(buf
, sizeof(buf
), fp
))
837 key
= strtok(buf
, " =\t\n");
838 val
= strtok(NULL
, "\n");
840 if (!key
|| !val
|| !*key
|| *key
== '#')
843 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
844 match
= !strcmp(ifname
, val
);
851 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
853 dest
= va_arg(ap_cur
, char *);
854 len
= va_arg(ap_cur
, int);
856 if (!strcmp(search
, key
))
858 strncpy(dest
, val
, len
- 1);
874 #define nl80211_hostapd_query(ifname, ...) \
875 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
878 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
881 struct timeval tv
= { 0, 256000 };
886 memset(buf
, 0, blen
);
888 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
891 if (!FD_ISSET(sock
, &rfds
))
894 return recv(sock
, buf
, blen
- 1, 0);
897 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
899 struct sockaddr_un remote
= { 0 };
900 size_t remote_length
, local_length
;
902 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
906 remote
.sun_family
= AF_UNIX
;
907 remote_length
= sizeof(remote
.sun_family
) +
908 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
911 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
917 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
919 remote_length
= sizeof(remote
.sun_family
) +
920 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
922 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
929 local
->sun_family
= AF_UNIX
;
930 local_length
= sizeof(local
->sun_family
) +
931 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
933 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
942 static int __nl80211_wpactl_query(const char *ifname
, ...)
945 struct sockaddr_un local
= { 0 };
946 int len
, mode
, found
= 0, sock
= -1;
947 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
949 if (nl80211_get_mode(ifname
, &mode
))
952 if (mode
!= IWINFO_OPMODE_CLIENT
&&
953 mode
!= IWINFO_OPMODE_ADHOC
&&
954 mode
!= IWINFO_OPMODE_MESHPOINT
)
957 sock
= nl80211_wpactl_connect(ifname
, &local
);
962 va_start(ap
, ifname
);
964 /* clear all destination buffers */
967 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
969 dest
= va_arg(ap_cur
, char *);
970 len
= va_arg(ap_cur
, int);
972 memset(dest
, 0, len
);
977 send(sock
, "STATUS", 6, 0);
981 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
987 for (line
= strtok_r(buf
, "\n", &pos
);
989 line
= strtok_r(NULL
, "\n", &pos
))
991 key
= strtok(line
, "=");
992 val
= strtok(NULL
, "\n");
999 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
1001 dest
= va_arg(ap_cur
, char *);
1002 len
= va_arg(ap_cur
, int);
1004 if (!strcmp(search
, key
))
1006 strncpy(dest
, val
, len
- 1);
1021 unlink(local
.sun_path
);
1026 #define nl80211_wpactl_query(ifname, ...) \
1027 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1030 static char * nl80211_ifadd(const char *ifname
)
1032 char path
[PATH_MAX
];
1033 static char nif
[IFNAMSIZ
] = { 0 };
1034 struct nl80211_msg_conveyor
*req
;
1037 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
1040 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
1042 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
1043 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
1045 nl80211_send(req
, NULL
, NULL
);
1047 snprintf(path
, sizeof(path
) - 1,
1048 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
1050 if ((sysfs
= fopen(path
, "w")) != NULL
)
1052 fwrite("0\n", 1, 2, sysfs
);
1065 static void nl80211_ifdel(const char *ifname
)
1067 struct nl80211_msg_conveyor
*req
;
1070 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1073 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1075 nl80211_send(req
, NULL
, NULL
);
1083 static void nl80211_hostapd_hup(const char *ifname
)
1087 char *phy
= nl80211_ifname2phy(ifname
);
1091 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1092 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1094 if (read(fd
, buf
, sizeof(buf
)) > 0)
1106 static int nl80211_probe(const char *ifname
)
1108 return !!nl80211_ifname2phy(ifname
);
1111 struct nl80211_ssid_bssid
{
1112 unsigned char *ssid
;
1113 unsigned char bssid
[7];
1116 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1118 struct nl80211_ssid_bssid
*sb
= arg
;
1119 struct nlattr
**tb
= nl80211_parse(msg
);
1121 if (tb
[NL80211_ATTR_MAC
]) {
1123 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1124 sizeof(sb
->bssid
) - 1);
1130 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1134 struct nl80211_ssid_bssid
*sb
= arg
;
1135 struct nlattr
**tb
= nl80211_parse(msg
);
1136 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1138 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1139 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1140 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1143 if (!tb
[NL80211_ATTR_BSS
] ||
1144 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1146 !bss
[NL80211_BSS_BSSID
] ||
1147 !bss
[NL80211_BSS_STATUS
] ||
1148 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1153 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1155 case NL80211_BSS_STATUS_ASSOCIATED
:
1156 case NL80211_BSS_STATUS_AUTHENTICATED
:
1157 case NL80211_BSS_STATUS_IBSS_JOINED
:
1161 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1162 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1164 while (ielen
>= 2 && ielen
>= ie
[1])
1168 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1179 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1188 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1191 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1193 /* try to find ssid from scan dump results */
1194 res
= nl80211_phy2ifname(ifname
);
1197 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1198 nl80211_get_ssid_bssid_cb
, &sb
);
1200 /* failed, try to find from hostapd info */
1201 if (sb
.ssid
[0] == 0)
1202 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1203 IWINFO_ESSID_MAX_SIZE
+ 1);
1205 /* failed, try to obtain Mesh ID */
1206 if (sb
.ssid
[0] == 0)
1207 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1208 sb
.ssid
, IWINFO_ESSID_MAX_SIZE
+ 1);
1210 return (sb
.ssid
[0] == 0) ? -1 : 0;
1213 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1215 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1216 struct nl80211_ssid_bssid sb
= { };
1218 res
= nl80211_phy2ifname(ifname
);
1220 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1221 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1222 nl80211_get_macaddr_cb
, &sb
);
1224 /* failed, try to find bssid from scan dump results */
1225 if (sb
.bssid
[0] == 0)
1226 nl80211_request(res
? res
: ifname
,
1227 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1228 nl80211_get_ssid_bssid_cb
, &sb
);
1230 /* failed, try to find mac from hostapd info */
1231 if ((sb
.bssid
[0] == 0) &&
1232 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1235 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1236 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1237 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1238 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1239 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1240 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1245 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1246 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1247 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1256 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1259 struct nlattr
**attr
= nl80211_parse(msg
);
1260 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1262 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1263 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1264 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1267 if (attr
[NL80211_ATTR_BSS
] &&
1268 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1269 attr
[NL80211_ATTR_BSS
], bss_policy
))
1271 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1272 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1278 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1281 struct nlattr
**tb
= nl80211_parse(msg
);
1283 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1284 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1289 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1291 char *res
, channel
[4], hwmode
[3];
1293 /* try to find frequency from interface info */
1294 res
= nl80211_phy2ifname(ifname
);
1297 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1298 nl80211_get_frequency_info_cb
, buf
);
1300 /* failed, try to find frequency from hostapd info */
1302 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1303 "channel", channel
, sizeof(channel
)) == 2)
1305 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1308 /* failed, try to find frequency from scan results */
1311 res
= nl80211_phy2ifname(ifname
);
1313 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1314 nl80211_get_frequency_scan_cb
, buf
);
1317 return (*buf
== 0) ? -1 : 0;
1320 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1323 struct nlattr
**tb
= nl80211_parse(msg
);
1325 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1326 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1331 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1335 /* try to find frequency from interface info */
1336 res
= nl80211_phy2ifname(ifname
);
1339 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1340 nl80211_get_center_freq1_cb
, buf
);
1342 return (*buf
== 0) ? -1 : 0;
1345 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1348 struct nlattr
**tb
= nl80211_parse(msg
);
1350 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1351 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1356 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1360 /* try to find frequency from interface info */
1361 res
= nl80211_phy2ifname(ifname
);
1364 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1365 nl80211_get_center_freq2_cb
, buf
);
1367 return (*buf
== 0) ? -1 : 0;
1370 static int nl80211_get_channel(const char *ifname
, int *buf
)
1372 if (!nl80211_get_frequency(ifname
, buf
))
1374 *buf
= nl80211_freq2channel(*buf
);
1381 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1383 if (!nl80211_get_center_freq1(ifname
, buf
))
1385 *buf
= nl80211_freq2channel(*buf
);
1392 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1394 if (!nl80211_get_center_freq2(ifname
, buf
))
1396 *buf
= nl80211_freq2channel(*buf
);
1403 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1406 struct nlattr
**tb
= nl80211_parse(msg
);
1408 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1409 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1414 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1418 res
= nl80211_phy2ifname(ifname
);
1421 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1422 nl80211_get_txpower_cb
, buf
))
1429 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1433 struct nl80211_rssi_rate
*rr
= arg
;
1434 struct nlattr
**attr
= nl80211_parse(msg
);
1435 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1436 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1438 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1439 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1440 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1441 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1442 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1443 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1444 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1445 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1446 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1447 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1448 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1451 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1452 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1453 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1454 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1455 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1458 if (attr
[NL80211_ATTR_STA_INFO
])
1460 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1461 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1463 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1465 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1466 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1470 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1472 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1473 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1476 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1478 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1479 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1490 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1495 memset(r
, 0, sizeof(*r
));
1497 if ((d
= opendir("/sys/class/net")) != NULL
)
1499 while ((de
= readdir(d
)) != NULL
)
1501 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1502 (!de
->d_name
[strlen(ifname
)] ||
1503 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1505 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1506 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1514 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1516 struct nl80211_rssi_rate rr
;
1518 nl80211_fill_signal(ifname
, &rr
);
1520 if (rr
.rate_samples
)
1522 *buf
= (rr
.rate
* 100);
1529 static int nl80211_get_signal(const char *ifname
, int *buf
)
1531 struct nl80211_rssi_rate rr
;
1533 nl80211_fill_signal(ifname
, &rr
);
1535 if (rr
.rssi_samples
)
1544 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1546 int8_t *noise
= arg
;
1547 struct nlattr
**tb
= nl80211_parse(msg
);
1548 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1550 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1551 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1552 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1555 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1558 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1559 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1562 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1565 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1566 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1572 static int nl80211_get_noise(const char *ifname
, int *buf
)
1576 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1577 nl80211_get_noise_cb
, &noise
))
1588 static int nl80211_get_quality(const char *ifname
, int *buf
)
1592 if (!nl80211_get_signal(ifname
, &signal
))
1594 /* A positive signal level is usually just a quality
1595 * value, pass through as-is */
1601 /* The cfg80211 wext compat layer assumes a signal range
1602 * of -110 dBm to -40 dBm, the quality value is derived
1603 * by adding 110 to the signal level */
1608 else if (signal
> -40)
1611 *buf
= (signal
+ 110);
1620 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1622 /* The cfg80211 wext compat layer assumes a maximum
1629 static int nl80211_check_wepkey(const char *key
)
1633 switch (strlen(key
))
1637 return IWINFO_CIPHER_WEP40
;
1641 return IWINFO_CIPHER_WEP104
;
1652 } wpa_key_mgmt_strings
[] = {
1653 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1654 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1655 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1656 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1657 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1658 { "NONE", 0, IWINFO_KMGMT_NONE
},
1659 { "None", 0, IWINFO_KMGMT_NONE
},
1660 { "PSK", 0, IWINFO_KMGMT_PSK
},
1661 { "EAP", 0, IWINFO_KMGMT_8021x
},
1662 { "SAE", 4, IWINFO_KMGMT_SAE
},
1663 { "OWE", 4, IWINFO_KMGMT_OWE
}
1666 static void parse_wpa_suites(const char *str
, int defversion
,
1667 uint8_t *versions
, uint8_t *suites
)
1671 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1677 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1679 m
= wpa_key_mgmt_strings
[i
].match
;
1682 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1684 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1685 version
= wpa_key_mgmt_strings
[i
].version
;
1687 version
= defversion
;
1689 *versions
|= version
;
1690 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1698 q
+= strcspn(q
, sep
);
1700 p
= q
+ strspn(q
, sep
);
1707 } wpa_cipher_strings
[] = {
1708 { "WEP-104", IWINFO_CIPHER_WEP104
},
1709 { "WEP-40", IWINFO_CIPHER_WEP40
},
1710 { "NONE", IWINFO_CIPHER_NONE
},
1711 { "TKIP", IWINFO_CIPHER_TKIP
},
1712 { "CCMP-256",IWINFO_CIPHER_CCMP256
},
1713 { "CCMP", IWINFO_CIPHER_CCMP
},
1714 { "GCMP-256",IWINFO_CIPHER_GCMP256
},
1715 { "GCMP", IWINFO_CIPHER_GCMP
}
1718 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1722 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1728 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1730 m
= wpa_cipher_strings
[i
].match
;
1733 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1735 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1743 q
+= strcspn(q
, sep
);
1745 p
= q
+ strspn(q
, sep
);
1749 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1753 uint8_t wpa_version
= 0;
1754 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1755 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1758 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1760 /* WPA supplicant */
1761 if (nl80211_wpactl_query(ifname
,
1762 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1763 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1764 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1765 "mode", mode
, sizeof(mode
)))
1768 if (!strcmp(wpa_key_mgmt
, "NONE"))
1770 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1771 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1773 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1775 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1776 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1779 c
->pair_ciphers
= 0;
1780 c
->group_ciphers
= 0;
1785 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1789 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1790 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1791 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1797 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1798 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1802 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1807 else if (!strncmp(p
, "WPA-", 4))
1813 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1815 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1822 else if (nl80211_hostapd_query(ifname
,
1823 "wpa", wpa
, sizeof(wpa
),
1824 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1825 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1826 "auth_algs", auth_algs
, sizeof(auth_algs
),
1827 "wep_key0", wep_key0
, sizeof(wep_key0
),
1828 "wep_key1", wep_key1
, sizeof(wep_key1
),
1829 "wep_key2", wep_key2
, sizeof(wep_key2
),
1830 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1834 if (wpa_key_mgmt
[0])
1836 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1838 if (!strncmp(p
, "WPA-", 4))
1841 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1844 c
->enabled
= c
->wpa_version
? 1 : 0;
1847 if (wpa_pairwise
[0])
1848 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1852 switch (atoi(auth_algs
))
1855 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1859 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1863 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1864 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1868 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1869 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1870 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1871 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1873 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1876 c
->group_ciphers
= c
->pair_ciphers
;
1881 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1882 else if (!nl80211_get_mode(ifname
, &opmode
) &&
1883 (opmode
== IWINFO_OPMODE_ADHOC
||
1884 opmode
== IWINFO_OPMODE_MESHPOINT
))
1895 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1899 name
= nl80211_ifname2phy(ifname
);
1906 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1908 name
= nl80211_ifname2phy(name
);
1912 strcpy(buf
, ifname
);
1921 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1922 struct iwinfo_rate_entry
*re
)
1924 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1925 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1926 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1927 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1929 if (ri
[NL80211_RATE_INFO_HE_MCS
])
1932 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_MCS
]);
1934 if (ri
[NL80211_RATE_INFO_HE_NSS
])
1935 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_NSS
]);
1936 if (ri
[NL80211_RATE_INFO_HE_GI
])
1937 re
->he_gi
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_GI
]);
1938 if (ri
[NL80211_RATE_INFO_HE_DCM
])
1939 re
->he_dcm
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_DCM
]);
1941 else if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1944 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1946 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1947 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1949 else if (ri
[NL80211_RATE_INFO_MCS
])
1952 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1955 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1957 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1959 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1961 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1963 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1964 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
1969 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
1970 re
->is_short_gi
= 1;
1972 re
->is_40mhz
= (re
->mhz
== 40);
1975 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
1977 struct nl80211_array_buf
*arr
= arg
;
1978 struct iwinfo_survey_entry
*e
= arr
->buf
;
1979 struct nlattr
**attr
= nl80211_parse(msg
);
1980 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
1983 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1984 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1985 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1986 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
1987 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
1988 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
1989 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
1990 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
1993 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
1994 attr
[NL80211_ATTR_SURVEY_INFO
],
1999 /* advance to end of array */
2001 memset(e
, 0, sizeof(*e
));
2003 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
2004 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
2006 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
2007 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
2009 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
2010 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
2012 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
2013 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
2015 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
2016 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
2018 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
2019 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
2021 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
2022 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
2029 static void plink_state_to_str(char *dst
, unsigned state
)
2032 case NL80211_PLINK_LISTEN
:
2033 strcpy(dst
, "LISTEN");
2035 case NL80211_PLINK_OPN_SNT
:
2036 strcpy(dst
, "OPN_SNT");
2038 case NL80211_PLINK_OPN_RCVD
:
2039 strcpy(dst
, "OPN_RCVD");
2041 case NL80211_PLINK_CNF_RCVD
:
2042 strcpy(dst
, "CNF_RCVD");
2044 case NL80211_PLINK_ESTAB
:
2045 strcpy(dst
, "ESTAB");
2047 case NL80211_PLINK_HOLDING
:
2048 strcpy(dst
, "HOLDING");
2050 case NL80211_PLINK_BLOCKED
:
2051 strcpy(dst
, "BLOCKED");
2054 strcpy(dst
, "UNKNOWN");
2059 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
2061 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
2064 case NL80211_MESH_POWER_ACTIVE
:
2065 strcpy(dst
, "ACTIVE");
2067 case NL80211_MESH_POWER_LIGHT_SLEEP
:
2068 strcpy(dst
, "LIGHT SLEEP");
2070 case NL80211_MESH_POWER_DEEP_SLEEP
:
2071 strcpy(dst
, "DEEP SLEEP");
2074 strcpy(dst
, "UNKNOWN");
2079 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2081 struct nl80211_array_buf
*arr
= arg
;
2082 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2083 struct nlattr
**attr
= nl80211_parse(msg
);
2084 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2085 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2086 struct nl80211_sta_flag_update
*sta_flags
;
2088 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2089 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2090 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2091 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2092 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2093 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2094 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2095 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2096 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2097 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2098 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2099 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2100 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2101 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2102 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2103 [NL80211_STA_INFO_STA_FLAGS
] =
2104 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2105 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2107 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2108 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2109 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2110 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2111 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2112 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2115 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2116 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2117 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2118 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2119 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2122 /* advance to end of array */
2124 memset(e
, 0, sizeof(*e
));
2126 if (attr
[NL80211_ATTR_MAC
])
2127 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2129 if (attr
[NL80211_ATTR_STA_INFO
] &&
2130 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2131 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2133 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2134 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2136 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2137 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2139 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2140 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2142 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2143 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2145 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2146 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2148 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2149 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2151 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2152 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2153 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2154 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2156 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2157 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2158 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2159 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2161 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2162 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2164 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2165 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2167 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2168 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2170 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2171 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2173 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2174 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2176 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2177 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2179 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2180 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2183 if (sinfo
[NL80211_STA_INFO_LLID
])
2184 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2186 if (sinfo
[NL80211_STA_INFO_PLID
])
2187 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2189 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2190 plink_state_to_str(e
->plink_state
,
2191 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2193 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2194 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2195 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2196 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2197 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2198 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2201 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2203 sta_flags
= (struct nl80211_sta_flag_update
*)
2204 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2206 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2207 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2208 e
->is_authorized
= 1;
2210 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2211 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2212 e
->is_authenticated
= 1;
2214 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2215 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2216 e
->is_preamble_short
= 1;
2218 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2219 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2222 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2223 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2226 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2227 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2232 e
->noise
= 0; /* filled in by caller */
2238 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2240 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2243 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2244 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2246 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2253 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2258 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2259 struct iwinfo_assoclist_entry
*e
;
2261 if ((d
= opendir("/sys/class/net")) != NULL
)
2263 while ((de
= readdir(d
)) != NULL
)
2265 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2266 (!de
->d_name
[strlen(ifname
)] ||
2267 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2269 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2270 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2276 if (!nl80211_get_noise(ifname
, &noise
))
2277 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2280 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2287 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2290 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2292 struct nlattr
**attr
= nl80211_parse(msg
);
2293 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2294 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2295 struct nlattr
*band
, *freq
;
2297 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2298 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2299 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2300 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2301 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2302 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2303 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2306 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2309 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2311 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2312 nla_len(band
), NULL
);
2314 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2316 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2317 nla_data(freq
), nla_len(freq
), freq_policy
);
2319 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2320 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2322 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2323 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2325 *dbm_max
= (int)(0.01 * nla_get_u32(
2326 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2336 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2339 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2340 struct nl80211_msg_conveyor
*req
;
2341 struct iwinfo_txpwrlist_entry entry
;
2343 if (nl80211_get_channel(ifname
, &ch_cur
))
2346 /* initialize the value pointer with channel for callback */
2349 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2350 nl80211_get_txpwrlist_cb
, &dbm_max
);
2354 for (dbm_cur
= 0, dbm_cnt
= 0;
2356 dbm_cur
++, dbm_cnt
++)
2358 entry
.dbm
= dbm_cur
;
2359 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2361 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2364 entry
.dbm
= dbm_max
;
2365 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2367 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2370 *len
= dbm_cnt
* sizeof(entry
);
2377 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2379 int wpa_version
= 0;
2380 char *p
, *q
, *proto
, *suites
;
2384 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2385 if (!strcmp(p
, "WEP")) {
2387 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2388 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2389 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2393 proto
= strtok(p
, "-");
2394 suites
= strtok(NULL
, "]");
2396 if (!proto
|| !suites
)
2399 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2401 else if (!strcmp(proto
, "WPA"))
2408 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2409 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2414 struct nl80211_scanlist
{
2415 struct iwinfo_scanlist_entry
*e
;
2420 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2421 struct iwinfo_scanlist_entry
*e
)
2423 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2424 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2425 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2428 while (ielen
>= 2 && ielen
>= ie
[1])
2433 case 114: /* Mesh ID */
2434 if (e
->ssid
[0] == 0) {
2435 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2436 memcpy(e
->ssid
, ie
+ 2, len
);
2442 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2443 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2446 case 221: /* Vendor */
2447 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2448 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2449 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2451 case 61: /* HT oeration */
2453 e
->ht_chan_info
.primary_chan
= ie
[2];
2454 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2455 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2458 case 192: /* VHT operation */
2460 e
->vht_chan_info
.chan_width
= ie
[2];
2461 e
->vht_chan_info
.center_chan_1
= ie
[3];
2462 e
->vht_chan_info
.center_chan_2
= ie
[4];
2472 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2477 struct nl80211_scanlist
*sl
= arg
;
2478 struct nlattr
**tb
= nl80211_parse(msg
);
2479 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2481 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2482 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2483 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2484 [NL80211_BSS_BSSID
] = { 0 },
2485 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2486 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2487 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2488 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2489 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2490 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2491 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2492 [NL80211_BSS_BEACON_IES
] = { 0 },
2495 if (!tb
[NL80211_ATTR_BSS
] ||
2496 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2498 !bss
[NL80211_BSS_BSSID
])
2503 if (bss
[NL80211_BSS_CAPABILITY
])
2504 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2508 memset(sl
->e
, 0, sizeof(*sl
->e
));
2509 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2512 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2513 else if (caps
& (1<<0))
2514 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2516 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2519 sl
->e
->crypto
.enabled
= 1;
2521 if (bss
[NL80211_BSS_FREQUENCY
])
2522 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2523 bss
[NL80211_BSS_FREQUENCY
]));
2525 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2526 nl80211_get_scanlist_ie(bss
, sl
->e
);
2528 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2531 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2533 rssi
= sl
->e
->signal
- 0x100;
2537 else if (rssi
> -40)
2540 sl
->e
->quality
= (rssi
+ 110);
2541 sl
->e
->quality_max
= 70;
2544 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2546 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2547 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2556 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2558 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2560 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2563 if (nl80211_wait("nl80211", "scan",
2564 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2567 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2568 nl80211_get_scanlist_cb
, &sl
))
2571 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2579 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2582 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2583 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2589 if (len
+ 1 >= outlen
)
2599 out
[len
++] = '\n'; in
++;
2603 out
[len
++] = '\r'; in
++;
2607 out
[len
++] = '\t'; in
++;
2611 out
[len
++] = '\033'; in
++;
2615 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2616 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2638 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2640 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2641 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2642 struct sockaddr_un local
= { 0 };
2643 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2645 sock
= nl80211_wpactl_connect(ifname
, &local
);
2650 send(sock
, "ATTACH", 6, 0);
2651 send(sock
, "SCAN", 4, 0);
2654 * wait for scan results:
2655 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2656 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2657 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2658 * time to process the results, so try 72 + 2 + 1 times.
2660 for (tries
= 0; tries
< 75; tries
++)
2662 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2665 /* got an event notification */
2666 if (reply
[0] == '<')
2668 /* scan results are ready */
2669 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2671 /* send "SCAN_RESULTS" command */
2672 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2676 /* is another unrelated event, retry */
2680 /* scanning already in progress, keep awaiting results */
2681 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2686 /* another failure, abort */
2687 else if (!strncmp(reply
, "FAIL-", 5))
2693 /* receive and parse scan results if the wait above didn't time out */
2694 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2696 /* received an event notification, receive again */
2697 if (reply
[0] == '<')
2700 nl80211_get_quality_max(ifname
, &qmax
);
2702 for (line
= strtok_r(reply
, "\n", &pos
);
2704 line
= strtok_r(NULL
, "\n", &pos
))
2706 /* skip header line */
2713 bssid
= strtok(line
, "\t");
2714 freq
= strtok(NULL
, "\t");
2715 signal
= strtok(NULL
, "\t");
2716 flags
= strtok(NULL
, "\t");
2717 ssid
= strtok(NULL
, "\n");
2719 if (!bssid
|| !freq
|| !signal
|| !flags
)
2723 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2724 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2725 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2726 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2727 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2728 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2732 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2737 if (strstr(flags
, "[MESH]"))
2738 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2739 else if (strstr(flags
, "[IBSS]"))
2740 e
->mode
= IWINFO_OPMODE_ADHOC
;
2742 e
->mode
= IWINFO_OPMODE_MASTER
;
2745 e
->channel
= nl80211_freq2channel(atoi(freq
));
2748 rssi
= atoi(signal
);
2754 /* The cfg80211 wext compat layer assumes a signal range
2755 * of -110 dBm to -40 dBm, the quality value is derived
2756 * by adding 110 to the signal level */
2759 else if (rssi
> -40)
2762 e
->quality
= (rssi
+ 110);
2770 e
->quality_max
= qmax
;
2773 nl80211_get_scancrypto(flags
, &e
->crypto
);
2779 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2784 unlink(local
.sun_path
);
2786 return (count
>= 0) ? 0 : -1;
2789 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2796 /* Got a radioX pseudo interface, find some interface on it or create one */
2797 if (!strncmp(ifname
, "radio", 5))
2799 /* Reuse existing interface */
2800 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2802 return nl80211_get_scanlist(res
, buf
, len
);
2805 /* Need to spawn a temporary iface for scanning */
2806 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2808 rv
= nl80211_get_scanlist(res
, buf
, len
);
2814 /* WPA supplicant */
2815 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2820 /* station / ad-hoc / monitor scan */
2821 else if (!nl80211_get_mode(ifname
, &mode
) &&
2822 (mode
== IWINFO_OPMODE_ADHOC
||
2823 mode
== IWINFO_OPMODE_MASTER
||
2824 mode
== IWINFO_OPMODE_CLIENT
||
2825 mode
== IWINFO_OPMODE_MONITOR
) &&
2826 iwinfo_ifup(ifname
))
2828 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2834 /* Got a temp interface, don't create yet another one */
2835 if (!strncmp(ifname
, "tmp.", 4))
2837 if (!iwinfo_ifup(ifname
))
2840 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2841 iwinfo_ifdown(ifname
);
2845 /* Spawn a new scan interface */
2848 if (!(res
= nl80211_ifadd(ifname
)))
2853 /* if we can take the new interface up, the driver supports an
2854 * additional interface and there's no need to tear down the ap */
2855 if (iwinfo_ifup(res
))
2857 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2861 /* driver cannot create secondary interface, take down ap
2863 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2865 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2867 iwinfo_ifup(ifname
);
2868 nl80211_hostapd_hup(ifname
);
2879 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2881 int bands_remain
, freqs_remain
;
2883 struct nl80211_array_buf
*arr
= arg
;
2884 struct iwinfo_freqlist_entry
*e
;
2886 struct nlattr
**attr
= nl80211_parse(msg
);
2887 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2888 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2889 struct nlattr
*band
, *freq
;
2894 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2895 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2897 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2898 nla_data(band
), nla_len(band
), NULL
);
2900 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2901 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2903 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2904 nla_data(freq
), nla_len(freq
), NULL
);
2906 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2907 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2910 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2911 e
->channel
= nl80211_freq2channel(e
->mhz
);
2914 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2915 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2918 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2919 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2920 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2921 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2922 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2923 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2924 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2925 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2926 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2927 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2928 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2929 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2941 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2943 struct nl80211_msg_conveyor
*cv
;
2944 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2945 uint32_t features
= nl80211_get_protocol_features(ifname
);
2948 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
2949 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
2953 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
2954 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
2957 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
2967 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
2970 struct nlattr
**attr
= nl80211_parse(msg
);
2972 if (attr
[NL80211_ATTR_REG_ALPHA2
])
2973 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
2980 static int nl80211_get_country(const char *ifname
, char *buf
)
2982 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
2983 nl80211_get_country_cb
, buf
))
2989 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
2992 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
2993 const struct iwinfo_iso3166_label
*l
;
2995 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
2997 e
->iso3166
= l
->iso3166
;
2998 e
->ccode
[0] = (l
->iso3166
/ 256);
2999 e
->ccode
[1] = (l
->iso3166
% 256);
3003 *len
= (count
* sizeof(struct iwinfo_country_entry
));
3008 struct nl80211_modes
3018 uint16_t he_phy_cap
[6];
3021 static int nl80211_eval_modelist(struct nl80211_modes
*m
)
3023 /* Treat any nonzero capability as 11n */
3026 m
->hw
|= IWINFO_80211_N
;
3027 m
->ht
|= IWINFO_HTMODE_HT20
;
3029 if (m
->nl_ht
& (1 << 1))
3030 m
->ht
|= IWINFO_HTMODE_HT40
;
3033 if (m
->he_phy_cap
[0] != 0) {
3034 m
->hw
|= IWINFO_80211_AX
;
3035 m
->ht
|= IWINFO_HTMODE_HE20
;
3037 if (m
->he_phy_cap
[0] & BIT(9))
3038 m
->ht
|= IWINFO_HTMODE_HE40
;
3039 if (m
->he_phy_cap
[0] & BIT(10))
3040 m
->ht
|= IWINFO_HTMODE_HE40
| IWINFO_HTMODE_HE80
;
3041 if (m
->he_phy_cap
[0] & BIT(11))
3042 m
->ht
|= IWINFO_HTMODE_HE160
;
3043 if (m
->he_phy_cap
[0] & BIT(12))
3044 m
->ht
|= IWINFO_HTMODE_HE160
| IWINFO_HTMODE_HE80_80
;
3047 if (m
->bands
& IWINFO_BAND_24
)
3049 m
->hw
|= IWINFO_80211_B
;
3050 m
->hw
|= IWINFO_80211_G
;
3053 if (m
->bands
& IWINFO_BAND_5
)
3055 /* Treat any nonzero capability as 11ac */
3058 m
->hw
|= IWINFO_80211_AC
;
3059 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3061 switch ((m
->nl_vht
>> 2) & 3)
3064 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3068 m
->ht
|= IWINFO_HTMODE_VHT160
;
3073 m
->hw
|= IWINFO_80211_A
;
3077 if (m
->bands
& IWINFO_BAND_60
)
3079 m
->hw
|= IWINFO_80211_AD
;
3084 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
3086 struct nl80211_modes
*m
= arg
;
3087 int bands_remain
, freqs_remain
;
3089 uint32_t vht_caps
= 0;
3090 struct nlattr
**attr
= nl80211_parse(msg
);
3091 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3092 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
3093 struct nlattr
*band
, *freq
;
3096 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
3098 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3100 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3101 nla_data(band
), nla_len(band
), NULL
);
3103 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
3104 m
->nl_ht
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
3106 if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
3107 m
->nl_vht
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
3109 if (bands
[NL80211_BAND_ATTR_IFTYPE_DATA
]) {
3110 struct nlattr
*tb
[NL80211_BAND_IFTYPE_ATTR_MAX
+ 1];
3111 struct nlattr
*nl_iftype
;
3115 nla_for_each_nested(nl_iftype
, bands
[NL80211_BAND_ATTR_IFTYPE_DATA
], rem_band
) {
3116 nla_parse(tb
, NL80211_BAND_IFTYPE_ATTR_MAX
,
3117 nla_data(nl_iftype
), nla_len(nl_iftype
), NULL
);
3118 if (tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]) {
3119 len
= nla_len(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]);
3121 if (len
> sizeof(m
->he_phy_cap
) - 1)
3122 len
= sizeof(m
->he_phy_cap
) - 1;
3123 memcpy(&((__u8
*)m
->he_phy_cap
)[1],
3124 nla_data(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]),
3130 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
3131 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
3134 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
3135 nla_data(freq
), nla_len(freq
), NULL
);
3137 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
3140 freq_mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
3142 if (freq_mhz
> 2400 && freq_mhz
< 2485)
3144 m
->bands
|= IWINFO_BAND_24
;
3146 else if (freq_mhz
> 5000 && freq_mhz
< 5850)
3148 m
->bands
|= IWINFO_BAND_5
;
3150 else if (freq_mhz
> 6000 && freq_mhz
< 7120)
3152 m
->bands
|= IWINFO_BAND_6
;
3154 else if (freq_mhz
>= 56160)
3156 m
->bands
|= IWINFO_BAND_60
;
3168 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3170 struct nl80211_msg_conveyor
*cv
;
3171 struct nl80211_modes m
= {};
3172 uint32_t features
= nl80211_get_protocol_features(ifname
);
3175 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3176 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3180 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3181 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3182 goto nla_put_failure
;
3184 nl80211_eval_modelist(&m
);
3201 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3203 struct nlattr
**tb
= nl80211_parse(msg
);
3205 struct chan_info
*chn
= arg
;
3207 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3208 chn
->width
= nla_get_u32(cur
);
3210 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3211 chn
->mode
= nla_get_u32(cur
);
3216 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3218 struct chan_info chn
= { .width
= 0, .mode
= 0 };
3222 res
= nl80211_phy2ifname(ifname
);
3225 err
= nl80211_request(res
? res
: ifname
,
3226 NL80211_CMD_GET_INTERFACE
, 0,
3227 nl80211_get_htmode_cb
, &chn
);
3231 switch (chn
.width
) {
3232 case NL80211_CHAN_WIDTH_20
:
3234 *buf
= IWINFO_HTMODE_VHT20
;
3236 *buf
= IWINFO_HTMODE_HT20
;
3238 case NL80211_CHAN_WIDTH_40
:
3240 *buf
= IWINFO_HTMODE_VHT40
;
3242 *buf
= IWINFO_HTMODE_HT40
;
3244 case NL80211_CHAN_WIDTH_80
:
3245 *buf
= IWINFO_HTMODE_VHT80
;
3247 case NL80211_CHAN_WIDTH_80P80
:
3248 *buf
= IWINFO_HTMODE_VHT80_80
;
3250 case NL80211_CHAN_WIDTH_160
:
3251 *buf
= IWINFO_HTMODE_VHT160
;
3253 case NL80211_CHAN_WIDTH_5
:
3254 case NL80211_CHAN_WIDTH_10
:
3255 case NL80211_CHAN_WIDTH_20_NOHT
:
3256 *buf
= IWINFO_HTMODE_NOHT
;
3265 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3267 struct nl80211_msg_conveyor
*cv
;
3268 struct nl80211_modes m
= {};
3269 uint32_t features
= nl80211_get_protocol_features(ifname
);
3272 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3273 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3277 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3278 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3279 goto nla_put_failure
;
3281 nl80211_eval_modelist(&m
);
3294 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3296 struct nlattr
**attr
= nl80211_parse(msg
);
3297 struct nlattr
*comb
;
3299 int comb_rem
, limit_rem
, mode_rem
;
3302 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3305 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3307 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3308 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3309 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3311 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3312 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3313 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3314 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3316 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3317 struct nlattr
*limit
;
3319 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3321 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3324 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3326 struct nlattr
*mode
;
3328 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3330 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3331 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3334 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3337 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3338 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3347 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3349 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3350 nl80211_get_ifcomb_cb
, buf
))
3356 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3358 char *phy
, compat
[64], path
[PATH_MAX
];
3361 /* Try to determine the phy name from the given interface */
3362 phy
= nl80211_ifname2phy(ifname
);
3364 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3365 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3367 if (nl80211_readstr(path
, compat
, sizeof(compat
)) <= 0)
3370 if (!strcmp(compat
, "qca,ar9130-wmac")) {
3371 id
->vendor_id
= 0x168c;
3372 id
->device_id
= 0x0029;
3373 id
->subsystem_vendor_id
= 0x168c;
3374 id
->subsystem_device_id
= 0x9130;
3375 } else if (!strcmp(compat
, "qca,ar9330-wmac")) {
3376 id
->vendor_id
= 0x168c;
3377 id
->device_id
= 0x0030;
3378 id
->subsystem_vendor_id
= 0x168c;
3379 id
->subsystem_device_id
= 0x9330;
3380 } else if (!strcmp(compat
, "qca,ar9340-wmac")) {
3381 id
->vendor_id
= 0x168c;
3382 id
->device_id
= 0x0030;
3383 id
->subsystem_vendor_id
= 0x168c;
3384 id
->subsystem_device_id
= 0x9340;
3385 } else if (!strcmp(compat
, "qca,qca9530-wmac")) {
3386 id
->vendor_id
= 0x168c;
3387 id
->device_id
= 0x0033;
3388 id
->subsystem_vendor_id
= 0x168c;
3389 id
->subsystem_device_id
= 0x9530;
3390 } else if (!strcmp(compat
, "qca,qca9550-wmac")) {
3391 id
->vendor_id
= 0x168c;
3392 id
->device_id
= 0x0033;
3393 id
->subsystem_vendor_id
= 0x168c;
3394 id
->subsystem_device_id
= 0x9550;
3395 } else if (!strcmp(compat
, "qca,qca9560-wmac")) {
3396 id
->vendor_id
= 0x168c;
3397 id
->device_id
= 0x0033;
3398 id
->subsystem_vendor_id
= 0x168c;
3399 id
->subsystem_device_id
= 0x9560;
3400 } else if (!strcmp(compat
, "qcom,ipq4019-wifi")) {
3401 id
->vendor_id
= 0x168c;
3402 id
->device_id
= 0x003c;
3403 id
->subsystem_vendor_id
= 0x168c;
3404 id
->subsystem_device_id
= 0x4019;
3405 } else if (!strcmp(compat
, "mediatek,mt7622-wmac")) {
3406 id
->vendor_id
= 0x14c3;
3407 id
->device_id
= 0x7622;
3408 id
->subsystem_vendor_id
= 0x14c3;
3409 id
->subsystem_device_id
= 0x7622;
3410 } else if (!strcmp(compat
, "mediatek,mt7986-wmac")) {
3411 id
->vendor_id
= 0x14c3;
3412 id
->device_id
= 0x7986;
3413 id
->subsystem_vendor_id
= 0x14c3;
3414 id
->subsystem_device_id
= 0x7986;
3417 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
3421 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3423 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3424 char *phy
, num
[8], path
[PATH_MAX
];
3427 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3428 { "vendor", &id
->vendor_id
},
3429 { "device", &id
->device_id
},
3430 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3431 { "subsystem_device", &id
->subsystem_device_id
}
3434 memset(id
, 0, sizeof(*id
));
3436 /* Try to determine the phy name from the given interface */
3437 phy
= nl80211_ifname2phy(ifname
);
3439 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3441 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3442 phy
? "ieee80211" : "net",
3443 phy
? phy
: ifname
, lookup
[i
].path
);
3445 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3446 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3449 /* Failed to obtain hardware IDs, try FDT */
3450 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3451 if (!nl80211_hardware_id_from_fdt(id
, ifname
))
3454 /* Failed to obtain hardware IDs, search board config */
3455 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3456 return iwinfo_hardware_id_from_mtd(id
);
3461 static const struct iwinfo_hardware_entry
*
3462 nl80211_get_hardware_entry(const char *ifname
)
3464 struct iwinfo_hardware_id id
;
3466 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3469 return iwinfo_hardware(&id
);
3472 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3474 const struct iwinfo_hardware_entry
*hw
;
3476 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3477 sprintf(buf
, "Generic MAC80211");
3479 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3484 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3486 const struct iwinfo_hardware_entry
*hw
;
3488 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3491 *buf
= hw
->txpower_offset
;
3495 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3497 const struct iwinfo_hardware_entry
*hw
;
3499 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3502 *buf
= hw
->frequency_offset
;
3506 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3510 if (!strncmp(section
, "path=", 5))
3511 idx
= nl80211_phy_idx_from_path(section
+ 5);
3512 else if (!strncmp(section
, "macaddr=", 8))
3513 idx
= nl80211_phy_idx_from_macaddr(section
+ 8);
3515 idx
= nl80211_phy_idx_from_uci(section
);
3520 sprintf(buf
, "phy%d", idx
);
3524 static int nl80211_phy_path(const char *phyname
, const char **path
)
3526 if (strchr(phyname
, '/'))
3529 *path
= nl80211_phy_path_str(phyname
);
3536 const struct iwinfo_ops nl80211_ops
= {
3538 .probe
= nl80211_probe
,
3539 .channel
= nl80211_get_channel
,
3540 .center_chan1
= nl80211_get_center_chan1
,
3541 .center_chan2
= nl80211_get_center_chan2
,
3542 .frequency
= nl80211_get_frequency
,
3543 .frequency_offset
= nl80211_get_frequency_offset
,
3544 .txpower
= nl80211_get_txpower
,
3545 .txpower_offset
= nl80211_get_txpower_offset
,
3546 .bitrate
= nl80211_get_bitrate
,
3547 .signal
= nl80211_get_signal
,
3548 .noise
= nl80211_get_noise
,
3549 .quality
= nl80211_get_quality
,
3550 .quality_max
= nl80211_get_quality_max
,
3551 .mbssid_support
= nl80211_get_mbssid_support
,
3552 .hwmodelist
= nl80211_get_hwmodelist
,
3553 .htmodelist
= nl80211_get_htmodelist
,
3554 .htmode
= nl80211_get_htmode
,
3555 .mode
= nl80211_get_mode
,
3556 .ssid
= nl80211_get_ssid
,
3557 .bssid
= nl80211_get_bssid
,
3558 .country
= nl80211_get_country
,
3559 .hardware_id
= nl80211_get_hardware_id
,
3560 .hardware_name
= nl80211_get_hardware_name
,
3561 .encryption
= nl80211_get_encryption
,
3562 .phyname
= nl80211_get_phyname
,
3563 .assoclist
= nl80211_get_assoclist
,
3564 .txpwrlist
= nl80211_get_txpwrlist
,
3565 .scanlist
= nl80211_get_scanlist
,
3566 .freqlist
= nl80211_get_freqlist
,
3567 .countrylist
= nl80211_get_countrylist
,
3568 .survey
= nl80211_get_survey
,
3569 .lookup_phy
= nl80211_lookup_phyname
,
3570 .phy_path
= nl80211_phy_path
,
3571 .close
= nl80211_close