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 if (strncmp(phyname
, "phy", 3) != 0)
251 phy_id
= atoi(phyname
+ 3);
252 buf_len
= snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/device", phyname
);
253 link
= realpath(buf
, path
);
257 if (strncmp(link
, prefix
, prefix_len
) != 0)
262 prefix
= "platform/";
263 prefix_len
= strlen(prefix
);
264 if (!strncmp(link
, prefix
, prefix_len
) && strstr(link
, "/pci"))
267 snprintf(buf
+ buf_len
, sizeof(buf
) - buf_len
, "/ieee80211");
272 while ((e
= readdir(d
)) != NULL
) {
275 if (strncmp(e
->d_name
, "phy", 3) != 0)
278 cur_id
= atoi(e
->d_name
+ 3);
279 if (cur_id
>= phy_id
)
290 offset
= link
- path
+ strlen(link
);
291 snprintf(path
+ offset
, sizeof(path
) - offset
, "+%d", seq
);
296 static int nl80211_phy_idx_from_path(const char *path
)
300 const char *cur_path
;
307 d
= opendir("/sys/class/ieee80211");
311 while ((e
= readdir(d
)) != NULL
) {
312 cur_path
= nl80211_phy_path_str(e
->d_name
);
316 if (strcmp(cur_path
, path
) != 0)
319 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
320 idx
= nl80211_readint(buf
);
331 static int nl80211_phy_idx_from_macaddr(const char *opt
)
340 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
341 if (glob(buf
, 0, NULL
, &gl
))
344 for (i
= 0; i
< gl
.gl_pathc
; i
++)
346 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
347 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
350 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
353 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
354 if ((idx
= nl80211_readint(buf
)) > -1)
363 static int nl80211_phy_idx_from_phy(const char *opt
)
370 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
371 return nl80211_readint(buf
);
374 static int nl80211_phy_idx_from_uci(const char *name
)
376 struct uci_section
*s
;
380 s
= iwinfo_uci_get_radio(name
, "mac80211");
384 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
385 idx
= nl80211_phy_idx_from_path(opt
);
389 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
390 idx
= nl80211_phy_idx_from_macaddr(opt
);
394 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
395 idx
= nl80211_phy_idx_from_phy(s
);
402 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
405 int ifidx
= -1, phyidx
= -1;
406 struct nl80211_msg_conveyor
*cv
;
411 if (nl80211_init() < 0)
414 if (!strncmp(ifname
, "phy", 3))
415 phyidx
= atoi(&ifname
[3]);
416 else if (!strncmp(ifname
, "radio", 5))
417 phyidx
= nl80211_phy_idx_from_uci(ifname
);
419 if (!strncmp(ifname
, "mon.", 4))
420 ifidx
= if_nametoindex(&ifname
[4]);
422 ifidx
= if_nametoindex(ifname
);
424 /* Valid ifidx must be greater than 0 */
425 if ((ifidx
<= 0) && (phyidx
< 0))
428 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
433 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
434 else if (phyidx
> -1)
435 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
444 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
445 int (*cb_func
)(struct nl_msg
*, void *),
448 static struct nl80211_msg_conveyor rcv
;
452 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
454 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
456 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
463 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
464 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
465 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
468 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
475 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
476 int (*cb_func
)(struct nl_msg
*, void *),
479 struct nl80211_msg_conveyor
*cv
;
481 cv
= nl80211_msg(ifname
, cmd
, flags
);
486 return nl80211_send(cv
, cb_func
, cb_arg
);
489 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
491 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
492 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
494 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
495 genlmsg_attrlen(gnlh
, 0), NULL
);
500 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
502 uint32_t *features
= arg
;
503 struct nlattr
**attr
= nl80211_parse(msg
);
505 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
506 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
511 static int nl80211_get_protocol_features(const char *ifname
)
513 struct nl80211_msg_conveyor
*req
;
514 uint32_t features
= 0;
516 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
518 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
525 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
527 struct nl80211_group_conveyor
*cv
= arg
;
529 struct nlattr
**attr
= nl80211_parse(msg
);
530 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
534 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
537 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
539 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
540 nla_data(mgrp
), nla_len(mgrp
), NULL
);
542 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
543 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
544 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
545 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
547 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
555 static int nl80211_subscribe(const char *family
, const char *group
)
557 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
558 struct nl80211_msg_conveyor
*req
;
561 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
564 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
565 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
570 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
580 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
582 struct nl80211_event_conveyor
*cv
= arg
;
583 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
585 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
586 cv
->recv
= gnlh
->cmd
;
591 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
596 static int __nl80211_wait(const char *family
, const char *group
, ...)
598 struct nl80211_event_conveyor cv
= { };
604 if (nl80211_subscribe(family
, group
))
607 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
612 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
613 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
614 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
618 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
619 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
623 while (!cv
.recv
&& !err
)
624 nl_recvmsgs(nls
->nl_sock
, cb
);
631 #define nl80211_wait(family, group, ...) \
632 __nl80211_wait(family, group, __VA_ARGS__, 0)
635 static int nl80211_freq2channel(int freq
)
639 else if (freq
< 2484)
640 return (freq
- 2407) / 5;
641 else if (freq
>= 4910 && freq
<= 4980)
642 return (freq
- 4000) / 5;
643 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
644 return (freq
- 56160) / 2160;
646 return (freq
- 5000) / 5;
649 static int nl80211_channel2freq(int channel
, const char *band
)
651 if (!band
|| band
[0] != 'a')
655 else if (channel
< 14)
656 return (channel
* 5) + 2407;
658 else if ( strcmp(band
, "ad") == 0)
660 return 56160 + 2160 * channel
;
664 if (channel
>= 182 && channel
<= 196)
665 return (channel
* 5) + 4000;
667 return (channel
* 5) + 5000;
673 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
676 struct nlattr
**attr
= nl80211_parse(msg
);
678 if (attr
[NL80211_ATTR_WIPHY_NAME
])
679 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
680 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
687 static char * nl80211_ifname2phy(const char *ifname
)
689 static char phy
[32] = { 0 };
691 memset(phy
, 0, sizeof(phy
));
693 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
694 nl80211_ifname2phy_cb
, phy
);
696 return phy
[0] ? phy
: NULL
;
699 static char * nl80211_phy2ifname(const char *ifname
)
701 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
703 static char nif
[IFNAMSIZ
] = { 0 };
708 /* Only accept phy name of the form phy%d or radio%d */
711 else if (!strncmp(ifname
, "phy", 3))
712 phyidx
= atoi(&ifname
[3]);
713 else if (!strncmp(ifname
, "radio", 5))
714 phyidx
= nl80211_phy_idx_from_uci(ifname
);
718 memset(nif
, 0, sizeof(nif
));
722 if ((d
= opendir("/sys/class/net")) != NULL
)
724 while ((e
= readdir(d
)) != NULL
)
726 snprintf(buffer
, sizeof(buffer
),
727 "/sys/class/net/%s/phy80211/index", e
->d_name
);
729 if (nl80211_readint(buffer
) == phyidx
)
731 snprintf(buffer
, sizeof(buffer
),
732 "/sys/class/net/%s/ifindex", e
->d_name
);
734 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
735 ((ifidx
< 0) || (cifidx
< ifidx
)))
738 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
747 return nif
[0] ? nif
: NULL
;
750 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
753 struct nlattr
**tb
= nl80211_parse(msg
);
754 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
755 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
756 IWINFO_OPMODE_ADHOC
, /* IBSS */
757 IWINFO_OPMODE_CLIENT
, /* managed */
758 IWINFO_OPMODE_MASTER
, /* AP */
759 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
760 IWINFO_OPMODE_WDS
, /* WDS */
761 IWINFO_OPMODE_MONITOR
, /* monitor */
762 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
763 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
764 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
767 if (tb
[NL80211_ATTR_IFTYPE
])
768 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
774 static int nl80211_get_mode(const char *ifname
, int *buf
)
778 *buf
= IWINFO_OPMODE_UNKNOWN
;
780 res
= nl80211_phy2ifname(ifname
);
782 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
783 nl80211_get_mode_cb
, buf
);
785 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
788 static int __nl80211_hostapd_query(const char *ifname
, ...)
791 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
792 int len
, mode
, found
= 0, match
= 1;
795 if (nl80211_get_mode(ifname
, &mode
))
798 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
801 phy
= nl80211_ifname2phy(ifname
);
806 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
807 fp
= fopen(buf
, "r");
812 va_start(ap
, ifname
);
814 /* clear all destination buffers */
817 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
819 dest
= va_arg(ap_cur
, char *);
820 len
= va_arg(ap_cur
, int);
822 memset(dest
, 0, len
);
827 /* iterate applicable lines and copy found values into dest buffers */
828 while (fgets(buf
, sizeof(buf
), fp
))
830 key
= strtok(buf
, " =\t\n");
831 val
= strtok(NULL
, "\n");
833 if (!key
|| !val
|| !*key
|| *key
== '#')
836 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
837 match
= !strcmp(ifname
, val
);
844 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
846 dest
= va_arg(ap_cur
, char *);
847 len
= va_arg(ap_cur
, int);
849 if (!strcmp(search
, key
))
851 strncpy(dest
, val
, len
- 1);
867 #define nl80211_hostapd_query(ifname, ...) \
868 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
871 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
874 struct timeval tv
= { 0, 256000 };
879 memset(buf
, 0, blen
);
881 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
884 if (!FD_ISSET(sock
, &rfds
))
887 return recv(sock
, buf
, blen
- 1, 0);
890 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
892 struct sockaddr_un remote
= { 0 };
893 size_t remote_length
, local_length
;
895 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
899 remote
.sun_family
= AF_UNIX
;
900 remote_length
= sizeof(remote
.sun_family
) +
901 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
904 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
910 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
912 remote_length
= sizeof(remote
.sun_family
) +
913 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
915 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
922 local
->sun_family
= AF_UNIX
;
923 local_length
= sizeof(local
->sun_family
) +
924 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
926 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
935 static int __nl80211_wpactl_query(const char *ifname
, ...)
938 struct sockaddr_un local
= { 0 };
939 int len
, mode
, found
= 0, sock
= -1;
940 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
942 if (nl80211_get_mode(ifname
, &mode
))
945 if (mode
!= IWINFO_OPMODE_CLIENT
&&
946 mode
!= IWINFO_OPMODE_ADHOC
&&
947 mode
!= IWINFO_OPMODE_MESHPOINT
)
950 sock
= nl80211_wpactl_connect(ifname
, &local
);
955 va_start(ap
, ifname
);
957 /* clear all destination buffers */
960 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
962 dest
= va_arg(ap_cur
, char *);
963 len
= va_arg(ap_cur
, int);
965 memset(dest
, 0, len
);
970 send(sock
, "STATUS", 6, 0);
974 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
980 for (line
= strtok_r(buf
, "\n", &pos
);
982 line
= strtok_r(NULL
, "\n", &pos
))
984 key
= strtok(line
, "=");
985 val
= strtok(NULL
, "\n");
992 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
994 dest
= va_arg(ap_cur
, char *);
995 len
= va_arg(ap_cur
, int);
997 if (!strcmp(search
, key
))
999 strncpy(dest
, val
, len
- 1);
1014 unlink(local
.sun_path
);
1019 #define nl80211_wpactl_query(ifname, ...) \
1020 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1023 static char * nl80211_ifadd(const char *ifname
)
1025 char path
[PATH_MAX
];
1026 static char nif
[IFNAMSIZ
] = { 0 };
1027 struct nl80211_msg_conveyor
*req
;
1030 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
1033 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
1035 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
1036 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
1038 nl80211_send(req
, NULL
, NULL
);
1040 snprintf(path
, sizeof(path
) - 1,
1041 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
1043 if ((sysfs
= fopen(path
, "w")) != NULL
)
1045 fwrite("0\n", 1, 2, sysfs
);
1058 static void nl80211_ifdel(const char *ifname
)
1060 struct nl80211_msg_conveyor
*req
;
1063 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1066 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1068 nl80211_send(req
, NULL
, NULL
);
1076 static void nl80211_hostapd_hup(const char *ifname
)
1080 char *phy
= nl80211_ifname2phy(ifname
);
1084 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1085 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1087 if (read(fd
, buf
, sizeof(buf
)) > 0)
1099 static int nl80211_probe(const char *ifname
)
1101 return !!nl80211_ifname2phy(ifname
);
1104 struct nl80211_ssid_bssid
{
1105 unsigned char *ssid
;
1106 unsigned char bssid
[7];
1109 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1111 struct nl80211_ssid_bssid
*sb
= arg
;
1112 struct nlattr
**tb
= nl80211_parse(msg
);
1114 if (tb
[NL80211_ATTR_MAC
]) {
1116 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1117 sizeof(sb
->bssid
) - 1);
1123 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1127 struct nl80211_ssid_bssid
*sb
= arg
;
1128 struct nlattr
**tb
= nl80211_parse(msg
);
1129 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1131 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1132 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1133 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1136 if (!tb
[NL80211_ATTR_BSS
] ||
1137 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1139 !bss
[NL80211_BSS_BSSID
] ||
1140 !bss
[NL80211_BSS_STATUS
] ||
1141 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1146 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1148 case NL80211_BSS_STATUS_ASSOCIATED
:
1149 case NL80211_BSS_STATUS_AUTHENTICATED
:
1150 case NL80211_BSS_STATUS_IBSS_JOINED
:
1154 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1155 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1157 while (ielen
>= 2 && ielen
>= ie
[1])
1161 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1172 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1181 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1184 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1186 /* try to find ssid from scan dump results */
1187 res
= nl80211_phy2ifname(ifname
);
1190 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1191 nl80211_get_ssid_bssid_cb
, &sb
);
1193 /* failed, try to find from hostapd info */
1194 if (sb
.ssid
[0] == 0)
1195 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1196 IWINFO_ESSID_MAX_SIZE
+ 1);
1198 /* failed, try to obtain Mesh ID */
1199 if (sb
.ssid
[0] == 0)
1200 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1201 sb
.ssid
, IWINFO_ESSID_MAX_SIZE
+ 1);
1203 return (sb
.ssid
[0] == 0) ? -1 : 0;
1206 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1208 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1209 struct nl80211_ssid_bssid sb
= { };
1211 res
= nl80211_phy2ifname(ifname
);
1213 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1214 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1215 nl80211_get_macaddr_cb
, &sb
);
1217 /* failed, try to find bssid from scan dump results */
1218 if (sb
.bssid
[0] == 0)
1219 nl80211_request(res
? res
: ifname
,
1220 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1221 nl80211_get_ssid_bssid_cb
, &sb
);
1223 /* failed, try to find mac from hostapd info */
1224 if ((sb
.bssid
[0] == 0) &&
1225 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1228 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1229 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1230 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1231 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1232 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1233 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1238 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1239 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1240 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1249 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1252 struct nlattr
**attr
= nl80211_parse(msg
);
1253 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1255 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1256 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1257 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1260 if (attr
[NL80211_ATTR_BSS
] &&
1261 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1262 attr
[NL80211_ATTR_BSS
], bss_policy
))
1264 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1265 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1271 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1274 struct nlattr
**tb
= nl80211_parse(msg
);
1276 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1277 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1282 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1284 char *res
, channel
[4], hwmode
[3];
1286 /* try to find frequency from interface info */
1287 res
= nl80211_phy2ifname(ifname
);
1290 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1291 nl80211_get_frequency_info_cb
, buf
);
1293 /* failed, try to find frequency from hostapd info */
1295 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1296 "channel", channel
, sizeof(channel
)) == 2)
1298 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1301 /* failed, try to find frequency from scan results */
1304 res
= nl80211_phy2ifname(ifname
);
1306 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1307 nl80211_get_frequency_scan_cb
, buf
);
1310 return (*buf
== 0) ? -1 : 0;
1313 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1316 struct nlattr
**tb
= nl80211_parse(msg
);
1318 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1319 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1324 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1328 /* try to find frequency from interface info */
1329 res
= nl80211_phy2ifname(ifname
);
1332 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1333 nl80211_get_center_freq1_cb
, buf
);
1335 return (*buf
== 0) ? -1 : 0;
1338 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1341 struct nlattr
**tb
= nl80211_parse(msg
);
1343 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1344 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1349 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1353 /* try to find frequency from interface info */
1354 res
= nl80211_phy2ifname(ifname
);
1357 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1358 nl80211_get_center_freq2_cb
, buf
);
1360 return (*buf
== 0) ? -1 : 0;
1363 static int nl80211_get_channel(const char *ifname
, int *buf
)
1365 if (!nl80211_get_frequency(ifname
, buf
))
1367 *buf
= nl80211_freq2channel(*buf
);
1374 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1376 if (!nl80211_get_center_freq1(ifname
, buf
))
1378 *buf
= nl80211_freq2channel(*buf
);
1385 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1387 if (!nl80211_get_center_freq2(ifname
, buf
))
1389 *buf
= nl80211_freq2channel(*buf
);
1396 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1399 struct nlattr
**tb
= nl80211_parse(msg
);
1401 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1402 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1407 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1411 res
= nl80211_phy2ifname(ifname
);
1414 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1415 nl80211_get_txpower_cb
, buf
))
1422 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1426 struct nl80211_rssi_rate
*rr
= arg
;
1427 struct nlattr
**attr
= nl80211_parse(msg
);
1428 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1429 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1431 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1432 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1433 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1434 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1435 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1436 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1437 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1438 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1439 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1440 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1441 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1444 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1445 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1446 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1447 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1448 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1451 if (attr
[NL80211_ATTR_STA_INFO
])
1453 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1454 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1456 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1458 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1459 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1463 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1465 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1466 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1469 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1471 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1472 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1483 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1488 memset(r
, 0, sizeof(*r
));
1490 if ((d
= opendir("/sys/class/net")) != NULL
)
1492 while ((de
= readdir(d
)) != NULL
)
1494 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1495 (!de
->d_name
[strlen(ifname
)] ||
1496 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1498 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1499 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1507 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1509 struct nl80211_rssi_rate rr
;
1511 nl80211_fill_signal(ifname
, &rr
);
1513 if (rr
.rate_samples
)
1515 *buf
= (rr
.rate
* 100);
1522 static int nl80211_get_signal(const char *ifname
, int *buf
)
1524 struct nl80211_rssi_rate rr
;
1526 nl80211_fill_signal(ifname
, &rr
);
1528 if (rr
.rssi_samples
)
1537 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1539 int8_t *noise
= arg
;
1540 struct nlattr
**tb
= nl80211_parse(msg
);
1541 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1543 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1544 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1545 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1548 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1551 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1552 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1555 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1558 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1559 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1565 static int nl80211_get_noise(const char *ifname
, int *buf
)
1569 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1570 nl80211_get_noise_cb
, &noise
))
1581 static int nl80211_get_quality(const char *ifname
, int *buf
)
1585 if (!nl80211_get_signal(ifname
, &signal
))
1587 /* A positive signal level is usually just a quality
1588 * value, pass through as-is */
1594 /* The cfg80211 wext compat layer assumes a signal range
1595 * of -110 dBm to -40 dBm, the quality value is derived
1596 * by adding 110 to the signal level */
1601 else if (signal
> -40)
1604 *buf
= (signal
+ 110);
1613 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1615 /* The cfg80211 wext compat layer assumes a maximum
1622 static int nl80211_check_wepkey(const char *key
)
1626 switch (strlen(key
))
1630 return IWINFO_CIPHER_WEP40
;
1634 return IWINFO_CIPHER_WEP104
;
1645 } wpa_key_mgmt_strings
[] = {
1646 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1647 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1648 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1649 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1650 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1651 { "NONE", 0, IWINFO_KMGMT_NONE
},
1652 { "None", 0, IWINFO_KMGMT_NONE
},
1653 { "PSK", 0, IWINFO_KMGMT_PSK
},
1654 { "EAP", 0, IWINFO_KMGMT_8021x
},
1655 { "SAE", 4, IWINFO_KMGMT_SAE
},
1656 { "OWE", 4, IWINFO_KMGMT_OWE
}
1659 static void parse_wpa_suites(const char *str
, int defversion
,
1660 uint8_t *versions
, uint8_t *suites
)
1664 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1670 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1672 m
= wpa_key_mgmt_strings
[i
].match
;
1675 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1677 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1678 version
= wpa_key_mgmt_strings
[i
].version
;
1680 version
= defversion
;
1682 *versions
|= version
;
1683 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1691 q
+= strcspn(q
, sep
);
1693 p
= q
+ strspn(q
, sep
);
1700 } wpa_cipher_strings
[] = {
1701 { "WEP-104", IWINFO_CIPHER_WEP104
},
1702 { "WEP-40", IWINFO_CIPHER_WEP40
},
1703 { "NONE", IWINFO_CIPHER_NONE
},
1704 { "TKIP", IWINFO_CIPHER_TKIP
},
1705 { "CCMP", IWINFO_CIPHER_CCMP
},
1706 { "GCMP", IWINFO_CIPHER_GCMP
}
1709 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1713 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1719 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1721 m
= wpa_cipher_strings
[i
].match
;
1724 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1726 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1734 q
+= strcspn(q
, sep
);
1736 p
= q
+ strspn(q
, sep
);
1740 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1744 uint8_t wpa_version
= 0;
1745 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1746 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1749 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1751 /* WPA supplicant */
1752 if (nl80211_wpactl_query(ifname
,
1753 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1754 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1755 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1756 "mode", mode
, sizeof(mode
)))
1759 if (!strcmp(wpa_key_mgmt
, "NONE"))
1761 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1762 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1764 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1766 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1767 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1770 c
->pair_ciphers
= 0;
1771 c
->group_ciphers
= 0;
1776 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1780 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1781 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1782 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1788 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1789 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1793 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1798 else if (!strncmp(p
, "WPA-", 4))
1804 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1806 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1813 else if (nl80211_hostapd_query(ifname
,
1814 "wpa", wpa
, sizeof(wpa
),
1815 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1816 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1817 "auth_algs", auth_algs
, sizeof(auth_algs
),
1818 "wep_key0", wep_key0
, sizeof(wep_key0
),
1819 "wep_key1", wep_key1
, sizeof(wep_key1
),
1820 "wep_key2", wep_key2
, sizeof(wep_key2
),
1821 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1825 if (wpa_key_mgmt
[0])
1827 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1829 if (!strncmp(p
, "WPA-", 4))
1832 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1835 c
->enabled
= c
->wpa_version
? 1 : 0;
1838 if (wpa_pairwise
[0])
1839 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1843 switch (atoi(auth_algs
))
1846 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1850 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1854 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1855 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1859 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1860 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1861 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1862 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1864 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1867 c
->group_ciphers
= c
->pair_ciphers
;
1872 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1873 else if (!nl80211_get_mode(ifname
, &opmode
) &&
1874 (opmode
== IWINFO_OPMODE_ADHOC
||
1875 opmode
== IWINFO_OPMODE_MESHPOINT
))
1886 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1890 name
= nl80211_ifname2phy(ifname
);
1897 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1899 name
= nl80211_ifname2phy(name
);
1903 strcpy(buf
, ifname
);
1912 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1913 struct iwinfo_rate_entry
*re
)
1915 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1916 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1917 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1918 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1920 if (ri
[NL80211_RATE_INFO_HE_MCS
])
1923 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_MCS
]);
1925 if (ri
[NL80211_RATE_INFO_HE_NSS
])
1926 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_NSS
]);
1927 if (ri
[NL80211_RATE_INFO_HE_GI
])
1928 re
->he_gi
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_GI
]);
1929 if (ri
[NL80211_RATE_INFO_HE_DCM
])
1930 re
->he_dcm
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_DCM
]);
1932 else if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1935 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1937 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1938 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1940 else if (ri
[NL80211_RATE_INFO_MCS
])
1943 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1946 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1948 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1950 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1952 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1954 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1955 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
1960 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
1961 re
->is_short_gi
= 1;
1963 re
->is_40mhz
= (re
->mhz
== 40);
1966 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
1968 struct nl80211_array_buf
*arr
= arg
;
1969 struct iwinfo_survey_entry
*e
= arr
->buf
;
1970 struct nlattr
**attr
= nl80211_parse(msg
);
1971 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
1974 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1975 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1976 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1977 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
1978 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
1979 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
1980 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
1981 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
1984 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
1985 attr
[NL80211_ATTR_SURVEY_INFO
],
1990 /* advance to end of array */
1992 memset(e
, 0, sizeof(*e
));
1994 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
1995 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
1997 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
1998 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
2000 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
2001 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
2003 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
2004 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
2006 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
2007 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
2009 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
2010 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
2012 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
2013 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
2020 static void plink_state_to_str(char *dst
, unsigned state
)
2023 case NL80211_PLINK_LISTEN
:
2024 strcpy(dst
, "LISTEN");
2026 case NL80211_PLINK_OPN_SNT
:
2027 strcpy(dst
, "OPN_SNT");
2029 case NL80211_PLINK_OPN_RCVD
:
2030 strcpy(dst
, "OPN_RCVD");
2032 case NL80211_PLINK_CNF_RCVD
:
2033 strcpy(dst
, "CNF_RCVD");
2035 case NL80211_PLINK_ESTAB
:
2036 strcpy(dst
, "ESTAB");
2038 case NL80211_PLINK_HOLDING
:
2039 strcpy(dst
, "HOLDING");
2041 case NL80211_PLINK_BLOCKED
:
2042 strcpy(dst
, "BLOCKED");
2045 strcpy(dst
, "UNKNOWN");
2050 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
2052 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
2055 case NL80211_MESH_POWER_ACTIVE
:
2056 strcpy(dst
, "ACTIVE");
2058 case NL80211_MESH_POWER_LIGHT_SLEEP
:
2059 strcpy(dst
, "LIGHT SLEEP");
2061 case NL80211_MESH_POWER_DEEP_SLEEP
:
2062 strcpy(dst
, "DEEP SLEEP");
2065 strcpy(dst
, "UNKNOWN");
2070 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2072 struct nl80211_array_buf
*arr
= arg
;
2073 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2074 struct nlattr
**attr
= nl80211_parse(msg
);
2075 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2076 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2077 struct nl80211_sta_flag_update
*sta_flags
;
2079 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2080 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2081 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2082 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2083 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2084 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2085 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2086 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2087 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2088 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2089 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2090 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2091 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2092 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2093 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2094 [NL80211_STA_INFO_STA_FLAGS
] =
2095 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2096 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2098 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2099 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2100 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2101 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2102 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2103 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2106 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2107 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2108 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2109 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2110 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2113 /* advance to end of array */
2115 memset(e
, 0, sizeof(*e
));
2117 if (attr
[NL80211_ATTR_MAC
])
2118 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2120 if (attr
[NL80211_ATTR_STA_INFO
] &&
2121 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2122 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2124 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2125 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2127 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2128 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2130 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2131 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2133 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2134 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2136 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2137 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2139 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2140 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2142 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2143 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2144 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2145 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2147 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2148 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2149 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2150 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2152 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2153 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2155 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2156 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2158 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2159 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2161 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2162 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2164 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2165 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2167 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2168 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2170 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2171 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2174 if (sinfo
[NL80211_STA_INFO_LLID
])
2175 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2177 if (sinfo
[NL80211_STA_INFO_PLID
])
2178 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2180 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2181 plink_state_to_str(e
->plink_state
,
2182 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2184 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2185 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2186 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2187 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2188 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2189 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2192 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2194 sta_flags
= (struct nl80211_sta_flag_update
*)
2195 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2197 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2198 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2199 e
->is_authorized
= 1;
2201 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2202 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2203 e
->is_authenticated
= 1;
2205 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2206 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2207 e
->is_preamble_short
= 1;
2209 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2210 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2213 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2214 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2217 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2218 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2223 e
->noise
= 0; /* filled in by caller */
2229 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2231 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2234 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2235 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2237 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2244 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2249 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2250 struct iwinfo_assoclist_entry
*e
;
2252 if ((d
= opendir("/sys/class/net")) != NULL
)
2254 while ((de
= readdir(d
)) != NULL
)
2256 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2257 (!de
->d_name
[strlen(ifname
)] ||
2258 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2260 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2261 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2267 if (!nl80211_get_noise(ifname
, &noise
))
2268 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2271 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2278 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2281 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2283 struct nlattr
**attr
= nl80211_parse(msg
);
2284 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2285 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2286 struct nlattr
*band
, *freq
;
2288 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2289 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2290 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2291 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2292 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2293 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2294 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2297 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2300 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2302 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2303 nla_len(band
), NULL
);
2305 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2307 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2308 nla_data(freq
), nla_len(freq
), freq_policy
);
2310 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2311 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2313 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2314 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2316 *dbm_max
= (int)(0.01 * nla_get_u32(
2317 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2327 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2330 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2331 struct nl80211_msg_conveyor
*req
;
2332 struct iwinfo_txpwrlist_entry entry
;
2334 if (nl80211_get_channel(ifname
, &ch_cur
))
2337 /* initialize the value pointer with channel for callback */
2340 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2341 nl80211_get_txpwrlist_cb
, &dbm_max
);
2345 for (dbm_cur
= 0, dbm_cnt
= 0;
2347 dbm_cur
++, dbm_cnt
++)
2349 entry
.dbm
= dbm_cur
;
2350 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2352 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2355 entry
.dbm
= dbm_max
;
2356 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2358 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2361 *len
= dbm_cnt
* sizeof(entry
);
2368 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2370 int wpa_version
= 0;
2371 char *p
, *q
, *proto
, *suites
;
2375 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2376 if (!strcmp(p
, "WEP")) {
2378 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2379 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2380 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2384 proto
= strtok(p
, "-");
2385 suites
= strtok(NULL
, "]");
2387 if (!proto
|| !suites
)
2390 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2392 else if (!strcmp(proto
, "WPA"))
2399 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2400 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2405 struct nl80211_scanlist
{
2406 struct iwinfo_scanlist_entry
*e
;
2411 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2412 struct iwinfo_scanlist_entry
*e
)
2414 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2415 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2416 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2419 while (ielen
>= 2 && ielen
>= ie
[1])
2424 case 114: /* Mesh ID */
2425 if (e
->ssid
[0] == 0) {
2426 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2427 memcpy(e
->ssid
, ie
+ 2, len
);
2433 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2434 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2437 case 221: /* Vendor */
2438 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2439 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2440 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2442 case 61: /* HT oeration */
2444 e
->ht_chan_info
.primary_chan
= ie
[2];
2445 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2446 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2449 case 192: /* VHT operation */
2451 e
->vht_chan_info
.chan_width
= ie
[2];
2452 e
->vht_chan_info
.center_chan_1
= ie
[3];
2453 e
->vht_chan_info
.center_chan_2
= ie
[4];
2463 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2468 struct nl80211_scanlist
*sl
= arg
;
2469 struct nlattr
**tb
= nl80211_parse(msg
);
2470 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2472 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2473 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2474 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2475 [NL80211_BSS_BSSID
] = { 0 },
2476 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2477 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2478 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2479 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2480 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2481 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2482 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2483 [NL80211_BSS_BEACON_IES
] = { 0 },
2486 if (!tb
[NL80211_ATTR_BSS
] ||
2487 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2489 !bss
[NL80211_BSS_BSSID
])
2494 if (bss
[NL80211_BSS_CAPABILITY
])
2495 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2499 memset(sl
->e
, 0, sizeof(*sl
->e
));
2500 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2503 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2504 else if (caps
& (1<<0))
2505 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2507 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2510 sl
->e
->crypto
.enabled
= 1;
2512 if (bss
[NL80211_BSS_FREQUENCY
])
2513 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2514 bss
[NL80211_BSS_FREQUENCY
]));
2516 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2517 nl80211_get_scanlist_ie(bss
, sl
->e
);
2519 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2522 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2524 rssi
= sl
->e
->signal
- 0x100;
2528 else if (rssi
> -40)
2531 sl
->e
->quality
= (rssi
+ 110);
2532 sl
->e
->quality_max
= 70;
2535 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2537 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2538 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2547 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2549 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2551 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2554 if (nl80211_wait("nl80211", "scan",
2555 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2558 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2559 nl80211_get_scanlist_cb
, &sl
))
2562 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2570 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2573 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2574 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2580 if (len
+ 1 >= outlen
)
2590 out
[len
++] = '\n'; in
++;
2594 out
[len
++] = '\r'; in
++;
2598 out
[len
++] = '\t'; in
++;
2602 out
[len
++] = '\033'; in
++;
2606 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2607 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2629 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2631 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2632 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2633 struct sockaddr_un local
= { 0 };
2634 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2636 sock
= nl80211_wpactl_connect(ifname
, &local
);
2641 send(sock
, "ATTACH", 6, 0);
2642 send(sock
, "SCAN", 4, 0);
2645 * wait for scan results:
2646 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2647 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2648 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2649 * time to process the results, so try 72 + 2 + 1 times.
2651 for (tries
= 0; tries
< 75; tries
++)
2653 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2656 /* got an event notification */
2657 if (reply
[0] == '<')
2659 /* scan results are ready */
2660 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2662 /* send "SCAN_RESULTS" command */
2663 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2667 /* is another unrelated event, retry */
2671 /* scanning already in progress, keep awaiting results */
2672 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2677 /* another failure, abort */
2678 else if (!strncmp(reply
, "FAIL-", 5))
2684 /* receive and parse scan results if the wait above didn't time out */
2685 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2687 /* received an event notification, receive again */
2688 if (reply
[0] == '<')
2691 nl80211_get_quality_max(ifname
, &qmax
);
2693 for (line
= strtok_r(reply
, "\n", &pos
);
2695 line
= strtok_r(NULL
, "\n", &pos
))
2697 /* skip header line */
2704 bssid
= strtok(line
, "\t");
2705 freq
= strtok(NULL
, "\t");
2706 signal
= strtok(NULL
, "\t");
2707 flags
= strtok(NULL
, "\t");
2708 ssid
= strtok(NULL
, "\n");
2710 if (!bssid
|| !freq
|| !signal
|| !flags
)
2714 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2715 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2716 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2717 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2718 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2719 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2723 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2728 if (strstr(flags
, "[MESH]"))
2729 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2730 else if (strstr(flags
, "[IBSS]"))
2731 e
->mode
= IWINFO_OPMODE_ADHOC
;
2733 e
->mode
= IWINFO_OPMODE_MASTER
;
2736 e
->channel
= nl80211_freq2channel(atoi(freq
));
2739 rssi
= atoi(signal
);
2745 /* The cfg80211 wext compat layer assumes a signal range
2746 * of -110 dBm to -40 dBm, the quality value is derived
2747 * by adding 110 to the signal level */
2750 else if (rssi
> -40)
2753 e
->quality
= (rssi
+ 110);
2761 e
->quality_max
= qmax
;
2764 nl80211_get_scancrypto(flags
, &e
->crypto
);
2770 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2775 unlink(local
.sun_path
);
2777 return (count
>= 0) ? 0 : -1;
2780 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2787 /* Got a radioX pseudo interface, find some interface on it or create one */
2788 if (!strncmp(ifname
, "radio", 5))
2790 /* Reuse existing interface */
2791 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2793 return nl80211_get_scanlist(res
, buf
, len
);
2796 /* Need to spawn a temporary iface for scanning */
2797 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2799 rv
= nl80211_get_scanlist(res
, buf
, len
);
2805 /* WPA supplicant */
2806 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2811 /* station / ad-hoc / monitor scan */
2812 else if (!nl80211_get_mode(ifname
, &mode
) &&
2813 (mode
== IWINFO_OPMODE_ADHOC
||
2814 mode
== IWINFO_OPMODE_MASTER
||
2815 mode
== IWINFO_OPMODE_CLIENT
||
2816 mode
== IWINFO_OPMODE_MONITOR
) &&
2817 iwinfo_ifup(ifname
))
2819 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2825 /* Got a temp interface, don't create yet another one */
2826 if (!strncmp(ifname
, "tmp.", 4))
2828 if (!iwinfo_ifup(ifname
))
2831 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2832 iwinfo_ifdown(ifname
);
2836 /* Spawn a new scan interface */
2839 if (!(res
= nl80211_ifadd(ifname
)))
2844 /* if we can take the new interface up, the driver supports an
2845 * additional interface and there's no need to tear down the ap */
2846 if (iwinfo_ifup(res
))
2848 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2852 /* driver cannot create secondary interface, take down ap
2854 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2856 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2858 iwinfo_ifup(ifname
);
2859 nl80211_hostapd_hup(ifname
);
2870 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2872 int bands_remain
, freqs_remain
;
2874 struct nl80211_array_buf
*arr
= arg
;
2875 struct iwinfo_freqlist_entry
*e
;
2877 struct nlattr
**attr
= nl80211_parse(msg
);
2878 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2879 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2880 struct nlattr
*band
, *freq
;
2885 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2886 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2888 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2889 nla_data(band
), nla_len(band
), NULL
);
2891 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2892 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2894 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2895 nla_data(freq
), nla_len(freq
), NULL
);
2897 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2898 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2901 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2902 e
->channel
= nl80211_freq2channel(e
->mhz
);
2905 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2906 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2909 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2910 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2911 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2912 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2913 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2914 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2915 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2916 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2917 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2918 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2919 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2920 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2932 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2934 struct nl80211_msg_conveyor
*cv
;
2935 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2936 uint32_t features
= nl80211_get_protocol_features(ifname
);
2939 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
2940 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
2944 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
2945 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
2948 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
2958 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
2961 struct nlattr
**attr
= nl80211_parse(msg
);
2963 if (attr
[NL80211_ATTR_REG_ALPHA2
])
2964 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
2971 static int nl80211_get_country(const char *ifname
, char *buf
)
2973 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
2974 nl80211_get_country_cb
, buf
))
2980 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
2983 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
2984 const struct iwinfo_iso3166_label
*l
;
2986 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
2988 e
->iso3166
= l
->iso3166
;
2989 e
->ccode
[0] = (l
->iso3166
/ 256);
2990 e
->ccode
[1] = (l
->iso3166
% 256);
2994 *len
= (count
* sizeof(struct iwinfo_country_entry
));
2999 struct nl80211_modes
3008 uint16_t he_phy_cap
[6];
3011 static int nl80211_eval_modelist(struct nl80211_modes
*m
)
3013 /* Treat any nonzero capability as 11n */
3016 m
->hw
|= IWINFO_80211_N
;
3017 m
->ht
|= IWINFO_HTMODE_HT20
;
3019 if (m
->nl_ht
& (1 << 1))
3020 m
->ht
|= IWINFO_HTMODE_HT40
;
3023 if (m
->he_phy_cap
[0] != 0) {
3024 m
->hw
|= IWINFO_80211_AX
;
3025 m
->ht
|= IWINFO_HTMODE_HE20
;
3027 if (m
->he_phy_cap
[0] & BIT(9))
3028 m
->ht
|= IWINFO_HTMODE_HE40
;
3029 if (m
->he_phy_cap
[0] & BIT(10))
3030 m
->ht
|= IWINFO_HTMODE_HE40
| IWINFO_HTMODE_HE80
;
3031 if (m
->he_phy_cap
[0] & BIT(11))
3032 m
->ht
|= IWINFO_HTMODE_HE160
;
3033 if (m
->he_phy_cap
[0] & BIT(12))
3034 m
->ht
|= IWINFO_HTMODE_HE160
| IWINFO_HTMODE_HE80_80
;
3037 if (m
->nl_freq
< 2485)
3039 m
->hw
|= IWINFO_80211_B
;
3040 m
->hw
|= IWINFO_80211_G
;
3044 /* Treat any nonzero capability as 11ac */
3047 m
->hw
|= IWINFO_80211_AC
;
3048 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3050 switch ((m
->nl_vht
>> 2) & 3)
3053 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3057 m
->ht
|= IWINFO_HTMODE_VHT160
;
3061 else if (m
->nl_freq
>= 56160)
3063 m
->hw
|= IWINFO_80211_AD
;
3065 else if (!(m
->hw
& IWINFO_80211_AC
))
3067 m
->hw
|= IWINFO_80211_A
;
3071 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
3073 struct nl80211_modes
*m
= arg
;
3074 int bands_remain
, freqs_remain
;
3076 uint32_t vht_caps
= 0;
3077 struct nlattr
**attr
= nl80211_parse(msg
);
3078 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3079 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
3080 struct nlattr
*band
, *freq
;
3082 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
3084 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3086 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3087 nla_data(band
), nla_len(band
), NULL
);
3089 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
3090 m
->nl_ht
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
3092 if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
3093 m
->nl_vht
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
3095 if (bands
[NL80211_BAND_ATTR_IFTYPE_DATA
]) {
3096 struct nlattr
*tb
[NL80211_BAND_IFTYPE_ATTR_MAX
+ 1];
3097 struct nlattr
*nl_iftype
;
3101 nla_for_each_nested(nl_iftype
, bands
[NL80211_BAND_ATTR_IFTYPE_DATA
], rem_band
) {
3102 nla_parse(tb
, NL80211_BAND_IFTYPE_ATTR_MAX
,
3103 nla_data(nl_iftype
), nla_len(nl_iftype
), NULL
);
3104 if (tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]) {
3105 len
= nla_len(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]);
3107 if (len
> sizeof(m
->he_phy_cap
) - 1)
3108 len
= sizeof(m
->he_phy_cap
) - 1;
3109 memcpy(&((__u8
*)m
->he_phy_cap
)[1],
3110 nla_data(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]),
3116 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
3117 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
3120 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
3121 nla_data(freq
), nla_len(freq
), NULL
);
3123 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
3126 m
->nl_freq
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
3137 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3139 struct nl80211_msg_conveyor
*cv
;
3140 struct nl80211_modes m
= {};
3141 uint32_t features
= nl80211_get_protocol_features(ifname
);
3144 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3145 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3149 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3150 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3151 goto nla_put_failure
;
3153 nl80211_eval_modelist(&m
);
3170 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3172 struct nlattr
**tb
= nl80211_parse(msg
);
3174 struct chan_info
*chn
= arg
;
3176 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3177 chn
->width
= nla_get_u32(cur
);
3179 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3180 chn
->mode
= nla_get_u32(cur
);
3185 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3187 struct chan_info chn
= { .width
= 0, .mode
= 0 };
3191 res
= nl80211_phy2ifname(ifname
);
3194 err
= nl80211_request(res
? res
: ifname
,
3195 NL80211_CMD_GET_INTERFACE
, 0,
3196 nl80211_get_htmode_cb
, &chn
);
3200 switch (chn
.width
) {
3201 case NL80211_CHAN_WIDTH_20
:
3203 *buf
= IWINFO_HTMODE_VHT20
;
3205 *buf
= IWINFO_HTMODE_HT20
;
3207 case NL80211_CHAN_WIDTH_40
:
3209 *buf
= IWINFO_HTMODE_VHT40
;
3211 *buf
= IWINFO_HTMODE_HT40
;
3213 case NL80211_CHAN_WIDTH_80
:
3214 *buf
= IWINFO_HTMODE_VHT80
;
3216 case NL80211_CHAN_WIDTH_80P80
:
3217 *buf
= IWINFO_HTMODE_VHT80_80
;
3219 case NL80211_CHAN_WIDTH_160
:
3220 *buf
= IWINFO_HTMODE_VHT160
;
3222 case NL80211_CHAN_WIDTH_5
:
3223 case NL80211_CHAN_WIDTH_10
:
3224 case NL80211_CHAN_WIDTH_20_NOHT
:
3225 *buf
= IWINFO_HTMODE_NOHT
;
3234 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3236 struct nl80211_msg_conveyor
*cv
;
3237 struct nl80211_modes m
= {};
3238 uint32_t features
= nl80211_get_protocol_features(ifname
);
3241 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3242 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3246 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3247 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3248 goto nla_put_failure
;
3250 nl80211_eval_modelist(&m
);
3263 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3265 struct nlattr
**attr
= nl80211_parse(msg
);
3266 struct nlattr
*comb
;
3268 int comb_rem
, limit_rem
, mode_rem
;
3271 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3274 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3276 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3277 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3278 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3280 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3281 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3282 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3283 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3285 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3286 struct nlattr
*limit
;
3288 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3290 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3293 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3295 struct nlattr
*mode
;
3297 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3299 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3300 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3303 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3306 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3307 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3316 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3318 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3319 nl80211_get_ifcomb_cb
, buf
))
3325 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3327 char *phy
, compat
[64], path
[PATH_MAX
];
3330 /* Try to determine the phy name from the given interface */
3331 phy
= nl80211_ifname2phy(ifname
);
3333 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3334 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3336 if (nl80211_readstr(path
, compat
, sizeof(compat
)) <= 0)
3339 if (!strcmp(compat
, "qca,ar9130-wmac")) {
3340 id
->vendor_id
= 0x168c;
3341 id
->device_id
= 0x0029;
3342 id
->subsystem_vendor_id
= 0x168c;
3343 id
->subsystem_device_id
= 0x9130;
3344 } else if (!strcmp(compat
, "qca,ar9330-wmac")) {
3345 id
->vendor_id
= 0x168c;
3346 id
->device_id
= 0x0030;
3347 id
->subsystem_vendor_id
= 0x168c;
3348 id
->subsystem_device_id
= 0x9330;
3349 } else if (!strcmp(compat
, "qca,ar9340-wmac")) {
3350 id
->vendor_id
= 0x168c;
3351 id
->device_id
= 0x0030;
3352 id
->subsystem_vendor_id
= 0x168c;
3353 id
->subsystem_device_id
= 0x9340;
3354 } else if (!strcmp(compat
, "qca,qca9530-wmac")) {
3355 id
->vendor_id
= 0x168c;
3356 id
->device_id
= 0x0033;
3357 id
->subsystem_vendor_id
= 0x168c;
3358 id
->subsystem_device_id
= 0x9530;
3359 } else if (!strcmp(compat
, "qca,qca9550-wmac")) {
3360 id
->vendor_id
= 0x168c;
3361 id
->device_id
= 0x0033;
3362 id
->subsystem_vendor_id
= 0x168c;
3363 id
->subsystem_device_id
= 0x9550;
3364 } else if (!strcmp(compat
, "qca,qca9560-wmac")) {
3365 id
->vendor_id
= 0x168c;
3366 id
->device_id
= 0x0033;
3367 id
->subsystem_vendor_id
= 0x168c;
3368 id
->subsystem_device_id
= 0x9560;
3369 } else if (!strcmp(compat
, "qcom,ipq4019-wifi")) {
3370 id
->vendor_id
= 0x168c;
3371 id
->device_id
= 0x003c;
3372 id
->subsystem_vendor_id
= 0x168c;
3373 id
->subsystem_device_id
= 0x4019;
3374 } else if (!strcmp(compat
, "mediatek,mt7622-wmac")) {
3375 id
->vendor_id
= 0x14c3;
3376 id
->device_id
= 0x7622;
3377 id
->subsystem_vendor_id
= 0x14c3;
3378 id
->subsystem_device_id
= 0x7622;
3380 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
3384 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3386 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3387 char *phy
, num
[8], path
[PATH_MAX
];
3390 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3391 { "vendor", &id
->vendor_id
},
3392 { "device", &id
->device_id
},
3393 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3394 { "subsystem_device", &id
->subsystem_device_id
}
3397 memset(id
, 0, sizeof(*id
));
3399 /* Try to determine the phy name from the given interface */
3400 phy
= nl80211_ifname2phy(ifname
);
3402 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3404 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3405 phy
? "ieee80211" : "net",
3406 phy
? phy
: ifname
, lookup
[i
].path
);
3408 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3409 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3412 /* Failed to obtain hardware IDs, try FDT */
3413 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3414 if (!nl80211_hardware_id_from_fdt(id
, ifname
))
3417 /* Failed to obtain hardware IDs, search board config */
3418 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3419 return iwinfo_hardware_id_from_mtd(id
);
3424 static const struct iwinfo_hardware_entry
*
3425 nl80211_get_hardware_entry(const char *ifname
)
3427 struct iwinfo_hardware_id id
;
3429 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3432 return iwinfo_hardware(&id
);
3435 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3437 const struct iwinfo_hardware_entry
*hw
;
3439 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3440 sprintf(buf
, "Generic MAC80211");
3442 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3447 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3449 const struct iwinfo_hardware_entry
*hw
;
3451 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3454 *buf
= hw
->txpower_offset
;
3458 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3460 const struct iwinfo_hardware_entry
*hw
;
3462 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3465 *buf
= hw
->frequency_offset
;
3469 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3473 if (!strncmp(section
, "path=", 5))
3474 idx
= nl80211_phy_idx_from_path(section
+ 5);
3475 else if (!strncmp(section
, "macaddr=", 8))
3476 idx
= nl80211_phy_idx_from_macaddr(section
+ 8);
3478 idx
= nl80211_phy_idx_from_uci(section
);
3483 sprintf(buf
, "phy%d", idx
);
3487 static int nl80211_phy_path(const char *phyname
, const char **path
)
3489 if (strncmp(phyname
, "phy", 3) != 0)
3492 if (strchr(phyname
, '/'))
3495 *path
= nl80211_phy_path_str(phyname
);
3502 const struct iwinfo_ops nl80211_ops
= {
3504 .probe
= nl80211_probe
,
3505 .channel
= nl80211_get_channel
,
3506 .center_chan1
= nl80211_get_center_chan1
,
3507 .center_chan2
= nl80211_get_center_chan2
,
3508 .frequency
= nl80211_get_frequency
,
3509 .frequency_offset
= nl80211_get_frequency_offset
,
3510 .txpower
= nl80211_get_txpower
,
3511 .txpower_offset
= nl80211_get_txpower_offset
,
3512 .bitrate
= nl80211_get_bitrate
,
3513 .signal
= nl80211_get_signal
,
3514 .noise
= nl80211_get_noise
,
3515 .quality
= nl80211_get_quality
,
3516 .quality_max
= nl80211_get_quality_max
,
3517 .mbssid_support
= nl80211_get_mbssid_support
,
3518 .hwmodelist
= nl80211_get_hwmodelist
,
3519 .htmodelist
= nl80211_get_htmodelist
,
3520 .htmode
= nl80211_get_htmode
,
3521 .mode
= nl80211_get_mode
,
3522 .ssid
= nl80211_get_ssid
,
3523 .bssid
= nl80211_get_bssid
,
3524 .country
= nl80211_get_country
,
3525 .hardware_id
= nl80211_get_hardware_id
,
3526 .hardware_name
= nl80211_get_hardware_name
,
3527 .encryption
= nl80211_get_encryption
,
3528 .phyname
= nl80211_get_phyname
,
3529 .assoclist
= nl80211_get_assoclist
,
3530 .txpwrlist
= nl80211_get_txpwrlist
,
3531 .scanlist
= nl80211_get_scanlist
,
3532 .freqlist
= nl80211_get_freqlist
,
3533 .countrylist
= nl80211_get_countrylist
,
3534 .survey
= nl80211_get_survey
,
3535 .lookup_phy
= nl80211_lookup_phyname
,
3536 .phy_path
= nl80211_phy_path
,
3537 .close
= nl80211_close