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.
30 #include "iwinfo_nl80211.h"
32 #define min(x, y) ((x) < (y)) ? (x) : (y)
34 #define BIT(x) (1ULL<<(x))
36 static struct nl80211_state
*nls
= NULL
;
38 static void nl80211_close(void)
43 genl_family_put(nls
->nlctrl
);
46 genl_family_put(nls
->nl80211
);
49 nl_socket_free(nls
->nl_sock
);
52 nl_cache_free(nls
->nl_cache
);
59 static int nl80211_init(void)
65 nls
= malloc(sizeof(struct nl80211_state
));
71 memset(nls
, 0, sizeof(*nls
));
73 nls
->nl_sock
= nl_socket_alloc();
79 if (genl_connect(nls
->nl_sock
)) {
84 fd
= nl_socket_get_fd(nls
->nl_sock
);
85 if (fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
) < 0) {
90 if (genl_ctrl_alloc_cache(nls
->nl_sock
, &nls
->nl_cache
)) {
95 nls
->nl80211
= genl_ctrl_search_by_name(nls
->nl_cache
, "nl80211");
101 nls
->nlctrl
= genl_ctrl_search_by_name(nls
->nl_cache
, "nlctrl");
116 static int nl80211_readint(const char *path
)
122 if ((fd
= open(path
, O_RDONLY
)) > -1)
124 if (read(fd
, buffer
, sizeof(buffer
)) > 0)
133 static int nl80211_readstr(const char *path
, char *buffer
, int length
)
138 if ((fd
= open(path
, O_RDONLY
)) > -1)
140 if ((rv
= read(fd
, buffer
, length
- 1)) > 0)
142 if (buffer
[rv
- 1] == '\n')
155 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
156 struct nlmsgerr
*err
, void *arg
)
163 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
170 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
177 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
182 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
197 static struct nl80211_msg_conveyor
* nl80211_new(struct genl_family
*family
,
200 static struct nl80211_msg_conveyor cv
;
202 struct nl_msg
*req
= NULL
;
203 struct nl_cb
*cb
= NULL
;
209 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
213 genlmsg_put(req
, 0, 0, genl_family_get_id(family
), 0, flags
, cmd
, 0);
227 static struct nl80211_msg_conveyor
* nl80211_ctl(int cmd
, int flags
)
229 if (nl80211_init() < 0)
232 return nl80211_new(nls
->nlctrl
, cmd
, flags
);
235 static int nl80211_phy_idx_from_uci_path(struct uci_section
*s
)
242 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
246 snprintf(buf
, sizeof(buf
), "/sys/devices/%s/ieee80211/*/index", opt
); /**/
247 if (glob(buf
, 0, NULL
, &gl
))
248 snprintf(buf
, sizeof(buf
), "/sys/devices/platform/%s/ieee80211/*/index", opt
); /**/
250 if (glob(buf
, 0, NULL
, &gl
))
254 idx
= nl80211_readint(gl
.gl_pathv
[0]);
261 static int nl80211_phy_idx_from_uci_macaddr(struct uci_section
*s
)
268 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
272 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
273 if (glob(buf
, 0, NULL
, &gl
))
276 for (i
= 0; i
< gl
.gl_pathc
; i
++)
278 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
279 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
282 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
285 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
286 if ((idx
= nl80211_readint(buf
)) > -1)
295 static int nl80211_phy_idx_from_uci_phy(struct uci_section
*s
)
300 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
304 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
305 return nl80211_readint(buf
);
308 static int nl80211_phy_idx_from_uci(const char *name
)
310 struct uci_section
*s
;
313 s
= iwinfo_uci_get_radio(name
, "mac80211");
317 idx
= nl80211_phy_idx_from_uci_path(s
);
320 idx
= nl80211_phy_idx_from_uci_macaddr(s
);
323 idx
= nl80211_phy_idx_from_uci_phy(s
);
330 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
333 int ifidx
= -1, phyidx
= -1;
334 struct nl80211_msg_conveyor
*cv
;
339 if (nl80211_init() < 0)
342 if (!strncmp(ifname
, "phy", 3))
343 phyidx
= atoi(&ifname
[3]);
344 else if (!strncmp(ifname
, "radio", 5))
345 phyidx
= nl80211_phy_idx_from_uci(ifname
);
346 else if (!strncmp(ifname
, "mon.", 4))
347 ifidx
= if_nametoindex(&ifname
[4]);
349 ifidx
= if_nametoindex(ifname
);
351 /* Valid ifidx must be greater than 0 */
352 if ((ifidx
<= 0) && (phyidx
< 0))
355 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
360 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
363 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
372 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
373 int (*cb_func
)(struct nl_msg
*, void *),
376 static struct nl80211_msg_conveyor rcv
;
380 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
382 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
384 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
391 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
392 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
393 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
396 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
403 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
404 int (*cb_func
)(struct nl_msg
*, void *),
407 struct nl80211_msg_conveyor
*cv
;
409 cv
= nl80211_msg(ifname
, cmd
, flags
);
414 return nl80211_send(cv
, cb_func
, cb_arg
);
417 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
419 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
420 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
422 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
423 genlmsg_attrlen(gnlh
, 0), NULL
);
428 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
430 uint32_t *features
= arg
;
431 struct nlattr
**attr
= nl80211_parse(msg
);
433 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
434 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
439 static int nl80211_get_protocol_features(const char *ifname
)
441 struct nl80211_msg_conveyor
*req
;
442 uint32_t features
= 0;
444 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
446 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
453 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
455 struct nl80211_group_conveyor
*cv
= arg
;
457 struct nlattr
**attr
= nl80211_parse(msg
);
458 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
462 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
465 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
467 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
468 nla_data(mgrp
), nla_len(mgrp
), NULL
);
470 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
471 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
472 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
473 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
475 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
483 static int nl80211_subscribe(const char *family
, const char *group
)
485 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
486 struct nl80211_msg_conveyor
*req
;
489 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
492 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
493 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
498 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
508 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
510 struct nl80211_event_conveyor
*cv
= arg
;
511 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
513 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
514 cv
->recv
= gnlh
->cmd
;
519 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
524 static int __nl80211_wait(const char *family
, const char *group
, ...)
526 struct nl80211_event_conveyor cv
= { };
532 if (nl80211_subscribe(family
, group
))
535 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
540 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
541 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
542 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
546 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
547 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
551 while (!cv
.recv
&& !err
)
552 nl_recvmsgs(nls
->nl_sock
, cb
);
559 #define nl80211_wait(family, group, ...) \
560 __nl80211_wait(family, group, __VA_ARGS__, 0)
563 static int nl80211_freq2channel(int freq
)
567 else if (freq
< 2484)
568 return (freq
- 2407) / 5;
569 else if (freq
>= 4910 && freq
<= 4980)
570 return (freq
- 4000) / 5;
571 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
572 return (freq
- 56160) / 2160;
574 return (freq
- 5000) / 5;
577 static int nl80211_channel2freq(int channel
, const char *band
)
579 if (!band
|| band
[0] != 'a')
583 else if (channel
< 14)
584 return (channel
* 5) + 2407;
586 else if ( strcmp(band
, "ad") == 0)
588 return 56160 + 2160 * channel
;
592 if (channel
>= 182 && channel
<= 196)
593 return (channel
* 5) + 4000;
595 return (channel
* 5) + 5000;
601 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
604 struct nlattr
**attr
= nl80211_parse(msg
);
606 if (attr
[NL80211_ATTR_WIPHY_NAME
])
607 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
608 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
615 static char * nl80211_ifname2phy(const char *ifname
)
617 static char phy
[32] = { 0 };
619 memset(phy
, 0, sizeof(phy
));
621 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
622 nl80211_ifname2phy_cb
, phy
);
624 return phy
[0] ? phy
: NULL
;
627 static char * nl80211_phy2ifname(const char *ifname
)
629 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
631 static char nif
[IFNAMSIZ
] = { 0 };
636 /* Only accept phy name of the form phy%d or radio%d */
639 else if (!strncmp(ifname
, "phy", 3))
640 phyidx
= atoi(&ifname
[3]);
641 else if (!strncmp(ifname
, "radio", 5))
642 phyidx
= nl80211_phy_idx_from_uci(ifname
);
646 memset(nif
, 0, sizeof(nif
));
650 if ((d
= opendir("/sys/class/net")) != NULL
)
652 while ((e
= readdir(d
)) != NULL
)
654 snprintf(buffer
, sizeof(buffer
),
655 "/sys/class/net/%s/phy80211/index", e
->d_name
);
657 if (nl80211_readint(buffer
) == phyidx
)
659 snprintf(buffer
, sizeof(buffer
),
660 "/sys/class/net/%s/ifindex", e
->d_name
);
662 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
663 ((ifidx
< 0) || (cifidx
< ifidx
)))
666 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
675 return nif
[0] ? nif
: NULL
;
678 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
681 struct nlattr
**tb
= nl80211_parse(msg
);
682 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
683 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
684 IWINFO_OPMODE_ADHOC
, /* IBSS */
685 IWINFO_OPMODE_CLIENT
, /* managed */
686 IWINFO_OPMODE_MASTER
, /* AP */
687 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
688 IWINFO_OPMODE_WDS
, /* WDS */
689 IWINFO_OPMODE_MONITOR
, /* monitor */
690 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
691 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
692 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
695 if (tb
[NL80211_ATTR_IFTYPE
])
696 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
702 static int nl80211_get_mode(const char *ifname
, int *buf
)
706 *buf
= IWINFO_OPMODE_UNKNOWN
;
708 res
= nl80211_phy2ifname(ifname
);
710 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
711 nl80211_get_mode_cb
, buf
);
713 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
716 static int __nl80211_hostapd_query(const char *ifname
, ...)
719 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
720 int len
, mode
, found
= 0, match
= 1;
723 if (nl80211_get_mode(ifname
, &mode
))
726 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
729 phy
= nl80211_ifname2phy(ifname
);
734 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
735 fp
= fopen(buf
, "r");
740 va_start(ap
, ifname
);
742 /* clear all destination buffers */
745 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
747 dest
= va_arg(ap_cur
, char *);
748 len
= va_arg(ap_cur
, int);
750 memset(dest
, 0, len
);
755 /* iterate applicable lines and copy found values into dest buffers */
756 while (fgets(buf
, sizeof(buf
), fp
))
758 key
= strtok(buf
, " =\t\n");
759 val
= strtok(NULL
, "\n");
761 if (!key
|| !val
|| !*key
|| *key
== '#')
764 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
765 match
= !strcmp(ifname
, val
);
772 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
774 dest
= va_arg(ap_cur
, char *);
775 len
= va_arg(ap_cur
, int);
777 if (!strcmp(search
, key
))
779 strncpy(dest
, val
, len
- 1);
795 #define nl80211_hostapd_query(ifname, ...) \
796 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
799 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
802 struct timeval tv
= { 0, 256000 };
807 memset(buf
, 0, blen
);
809 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
812 if (!FD_ISSET(sock
, &rfds
))
815 return recv(sock
, buf
, blen
- 1, 0);
818 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
820 struct sockaddr_un remote
= { 0 };
821 size_t remote_length
, local_length
;
823 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
827 remote
.sun_family
= AF_UNIX
;
828 remote_length
= sizeof(remote
.sun_family
) +
829 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
832 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
838 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
840 remote_length
= sizeof(remote
.sun_family
) +
841 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
843 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
850 local
->sun_family
= AF_UNIX
;
851 local_length
= sizeof(local
->sun_family
) +
852 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
854 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
863 static int __nl80211_wpactl_query(const char *ifname
, ...)
866 struct sockaddr_un local
= { 0 };
867 int len
, mode
, found
= 0, sock
= -1;
868 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
870 if (nl80211_get_mode(ifname
, &mode
))
873 if (mode
!= IWINFO_OPMODE_CLIENT
&& mode
!= IWINFO_OPMODE_ADHOC
)
876 sock
= nl80211_wpactl_connect(ifname
, &local
);
881 va_start(ap
, ifname
);
883 /* clear all destination buffers */
886 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
888 dest
= va_arg(ap_cur
, char *);
889 len
= va_arg(ap_cur
, int);
891 memset(dest
, 0, len
);
896 send(sock
, "STATUS", 6, 0);
900 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
906 for (line
= strtok_r(buf
, "\n", &pos
);
908 line
= strtok_r(NULL
, "\n", &pos
))
910 key
= strtok(line
, "=");
911 val
= strtok(NULL
, "\n");
918 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
920 dest
= va_arg(ap_cur
, char *);
921 len
= va_arg(ap_cur
, int);
923 if (!strcmp(search
, key
))
925 strncpy(dest
, val
, len
- 1);
940 unlink(local
.sun_path
);
945 #define nl80211_wpactl_query(ifname, ...) \
946 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
949 static char * nl80211_ifadd(const char *ifname
)
952 static char nif
[IFNAMSIZ
] = { 0 };
953 struct nl80211_msg_conveyor
*req
;
956 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
959 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
961 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
962 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
964 nl80211_send(req
, NULL
, NULL
);
966 snprintf(path
, sizeof(path
) - 1,
967 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
969 if ((sysfs
= fopen(path
, "w")) != NULL
)
971 fwrite("0\n", 1, 2, sysfs
);
984 static void nl80211_ifdel(const char *ifname
)
986 struct nl80211_msg_conveyor
*req
;
989 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
992 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
994 nl80211_send(req
, NULL
, NULL
);
1002 static void nl80211_hostapd_hup(const char *ifname
)
1006 char *phy
= nl80211_ifname2phy(ifname
);
1010 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1011 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1013 if (read(fd
, buf
, sizeof(buf
)) > 0)
1025 static int nl80211_probe(const char *ifname
)
1027 return !!nl80211_ifname2phy(ifname
);
1030 struct nl80211_ssid_bssid
{
1031 unsigned char *ssid
;
1032 unsigned char bssid
[7];
1035 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1037 struct nl80211_ssid_bssid
*sb
= arg
;
1038 struct nlattr
**tb
= nl80211_parse(msg
);
1040 if (tb
[NL80211_ATTR_MAC
]) {
1042 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1043 sizeof(sb
->bssid
) - 1);
1049 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1053 struct nl80211_ssid_bssid
*sb
= arg
;
1054 struct nlattr
**tb
= nl80211_parse(msg
);
1055 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1057 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1058 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1059 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1062 if (!tb
[NL80211_ATTR_BSS
] ||
1063 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1065 !bss
[NL80211_BSS_BSSID
] ||
1066 !bss
[NL80211_BSS_STATUS
] ||
1067 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1072 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1074 case NL80211_BSS_STATUS_ASSOCIATED
:
1075 case NL80211_BSS_STATUS_AUTHENTICATED
:
1076 case NL80211_BSS_STATUS_IBSS_JOINED
:
1080 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1081 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1083 while (ielen
>= 2 && ielen
>= ie
[1])
1087 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1098 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1107 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1110 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1112 /* try to find ssid from scan dump results */
1113 res
= nl80211_phy2ifname(ifname
);
1116 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1117 nl80211_get_ssid_bssid_cb
, &sb
);
1119 /* failed, try to find from hostapd info */
1120 if (sb
.ssid
[0] == 0)
1121 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1122 IWINFO_ESSID_MAX_SIZE
+ 1);
1124 /* failed, try to obtain Mesh ID */
1125 if (sb
.ssid
[0] == 0)
1126 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1127 sb
.ssid
, IWINFO_ESSID_MAX_SIZE
+ 1);
1129 return (sb
.ssid
[0] == 0) ? -1 : 0;
1132 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1134 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1135 struct nl80211_ssid_bssid sb
= { };
1137 res
= nl80211_phy2ifname(ifname
);
1139 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1140 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1141 nl80211_get_macaddr_cb
, &sb
);
1143 /* failed, try to find bssid from scan dump results */
1144 if (sb
.bssid
[0] == 0)
1145 nl80211_request(res
? res
: ifname
,
1146 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1147 nl80211_get_ssid_bssid_cb
, &sb
);
1149 /* failed, try to find mac from hostapd info */
1150 if ((sb
.bssid
[0] == 0) &&
1151 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1154 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1155 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1156 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1157 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1158 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1159 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1164 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1165 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1166 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1175 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1178 struct nlattr
**attr
= nl80211_parse(msg
);
1179 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1181 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1182 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1183 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1186 if (attr
[NL80211_ATTR_BSS
] &&
1187 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1188 attr
[NL80211_ATTR_BSS
], bss_policy
))
1190 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1191 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1197 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1200 struct nlattr
**tb
= nl80211_parse(msg
);
1202 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1203 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1208 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1210 char *res
, channel
[4], hwmode
[3];
1212 /* try to find frequency from interface info */
1213 res
= nl80211_phy2ifname(ifname
);
1216 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1217 nl80211_get_frequency_info_cb
, buf
);
1219 /* failed, try to find frequency from hostapd info */
1221 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1222 "channel", channel
, sizeof(channel
)) == 2)
1224 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1227 /* failed, try to find frequency from scan results */
1230 res
= nl80211_phy2ifname(ifname
);
1232 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1233 nl80211_get_frequency_scan_cb
, buf
);
1236 return (*buf
== 0) ? -1 : 0;
1239 static int nl80211_get_channel(const char *ifname
, int *buf
)
1241 if (!nl80211_get_frequency(ifname
, buf
))
1243 *buf
= nl80211_freq2channel(*buf
);
1250 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1253 struct nlattr
**tb
= nl80211_parse(msg
);
1255 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1256 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1261 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1265 res
= nl80211_phy2ifname(ifname
);
1268 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1269 nl80211_get_txpower_cb
, buf
))
1276 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1280 struct nl80211_rssi_rate
*rr
= arg
;
1281 struct nlattr
**attr
= nl80211_parse(msg
);
1282 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1283 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1285 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1286 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1287 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1288 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1289 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1290 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1291 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1292 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1293 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1294 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1295 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1298 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1299 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1300 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1301 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1302 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1305 if (attr
[NL80211_ATTR_STA_INFO
])
1307 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1308 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1310 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1312 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1313 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1317 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1319 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1320 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1323 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1325 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1326 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1337 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1342 memset(r
, 0, sizeof(*r
));
1344 if ((d
= opendir("/sys/class/net")) != NULL
)
1346 while ((de
= readdir(d
)) != NULL
)
1348 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1349 (!de
->d_name
[strlen(ifname
)] ||
1350 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1352 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1353 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1361 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1363 struct nl80211_rssi_rate rr
;
1365 nl80211_fill_signal(ifname
, &rr
);
1367 if (rr
.rate_samples
)
1369 *buf
= (rr
.rate
* 100);
1376 static int nl80211_get_signal(const char *ifname
, int *buf
)
1378 struct nl80211_rssi_rate rr
;
1380 nl80211_fill_signal(ifname
, &rr
);
1382 if (rr
.rssi_samples
)
1391 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1393 int8_t *noise
= arg
;
1394 struct nlattr
**tb
= nl80211_parse(msg
);
1395 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1397 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1398 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1399 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1402 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1405 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1406 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1409 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1412 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1413 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1419 static int nl80211_get_noise(const char *ifname
, int *buf
)
1423 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1424 nl80211_get_noise_cb
, &noise
))
1435 static int nl80211_get_quality(const char *ifname
, int *buf
)
1439 if (!nl80211_get_signal(ifname
, &signal
))
1441 /* A positive signal level is usually just a quality
1442 * value, pass through as-is */
1448 /* The cfg80211 wext compat layer assumes a signal range
1449 * of -110 dBm to -40 dBm, the quality value is derived
1450 * by adding 110 to the signal level */
1455 else if (signal
> -40)
1458 *buf
= (signal
+ 110);
1467 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1469 /* The cfg80211 wext compat layer assumes a maximum
1476 static int nl80211_check_wepkey(const char *key
)
1480 switch (strlen(key
))
1484 return IWINFO_CIPHER_WEP40
;
1488 return IWINFO_CIPHER_WEP104
;
1495 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1497 char wpa
[2], wpa_key_mgmt
[16], wpa_pairwise
[16], wpa_groupwise
[16];
1498 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1500 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1502 /* WPA supplicant */
1503 if (nl80211_wpactl_query(ifname
,
1504 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1505 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1506 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
)))
1509 if (!strcmp(wpa_key_mgmt
, "NONE"))
1511 if (strstr(wpa_pairwise
, "WEP-40"))
1512 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
1513 else if (strstr(wpa_pairwise
, "WEP-104"))
1514 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
1516 if (strstr(wpa_groupwise
, "WEP-40"))
1517 c
->group_ciphers
|= IWINFO_CIPHER_WEP40
;
1518 else if (strstr(wpa_groupwise
, "WEP-104"))
1519 c
->group_ciphers
|= IWINFO_CIPHER_WEP104
;
1521 c
->enabled
= !!(c
->pair_ciphers
| c
->group_ciphers
);
1522 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1523 c
->auth_algs
|= IWINFO_AUTH_OPEN
; /* XXX: assumption */
1527 else if (strstr(wpa_key_mgmt
, "WPA"))
1529 if (strstr(wpa_pairwise
, "TKIP"))
1530 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
1531 else if (strstr(wpa_pairwise
, "CCMP"))
1532 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
1533 else if (strstr(wpa_pairwise
, "NONE"))
1534 c
->pair_ciphers
|= IWINFO_CIPHER_NONE
;
1535 else if (strstr(wpa_pairwise
, "WEP-40"))
1536 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
1537 else if (strstr(wpa_pairwise
, "WEP-104"))
1538 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
1540 if (strstr(wpa_groupwise
, "TKIP"))
1541 c
->group_ciphers
|= IWINFO_CIPHER_TKIP
;
1542 else if (strstr(wpa_groupwise
, "CCMP"))
1543 c
->group_ciphers
|= IWINFO_CIPHER_CCMP
;
1544 else if (strstr(wpa_groupwise
, "NONE"))
1545 c
->group_ciphers
|= IWINFO_CIPHER_NONE
;
1546 else if (strstr(wpa_groupwise
, "WEP-40"))
1547 c
->group_ciphers
|= IWINFO_CIPHER_WEP40
;
1548 else if (strstr(wpa_groupwise
, "WEP-104"))
1549 c
->group_ciphers
|= IWINFO_CIPHER_WEP104
;
1551 if (strstr(wpa_key_mgmt
, "WPA2"))
1553 else if (strstr(wpa_key_mgmt
, "WPA"))
1556 if (strstr(wpa_key_mgmt
, "PSK"))
1557 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1558 else if (strstr(wpa_key_mgmt
, "EAP") ||
1559 strstr(wpa_key_mgmt
, "802.1X"))
1560 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
1561 else if (strstr(wpa_key_mgmt
, "NONE"))
1562 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1564 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1571 else if (nl80211_hostapd_query(ifname
,
1572 "wpa", wpa
, sizeof(wpa
),
1573 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1574 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1575 "auth_algs", auth_algs
, sizeof(auth_algs
),
1576 "wep_key0", wep_key0
, sizeof(wep_key0
),
1577 "wep_key1", wep_key1
, sizeof(wep_key1
),
1578 "wep_key2", wep_key2
, sizeof(wep_key2
),
1579 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1581 c
->wpa_version
= wpa
[0] ? atoi(wpa
) : 0;
1583 if (wpa_key_mgmt
[0])
1585 if (strstr(wpa_key_mgmt
, "PSK"))
1586 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1588 if (strstr(wpa_key_mgmt
, "EAP"))
1589 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
1591 if (strstr(wpa_key_mgmt
, "SAE")){
1592 c
->auth_suites
|= IWINFO_KMGMT_SAE
;
1596 if (strstr(wpa_key_mgmt
, "OWE")){
1597 c
->auth_suites
|= IWINFO_KMGMT_OWE
;
1601 if (strstr(wpa_key_mgmt
, "NONE"))
1602 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1606 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1609 if (wpa_pairwise
[0])
1611 if (strstr(wpa_pairwise
, "TKIP"))
1612 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
1614 if (strstr(wpa_pairwise
, "CCMP"))
1615 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
1617 if (strstr(wpa_pairwise
, "NONE"))
1618 c
->pair_ciphers
|= IWINFO_CIPHER_NONE
;
1623 switch(atoi(auth_algs
))
1626 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1630 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1634 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1635 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1639 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1640 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1641 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1642 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1645 c
->group_ciphers
= c
->pair_ciphers
;
1646 c
->enabled
= (c
->wpa_version
|| c
->pair_ciphers
) ? 1 : 0;
1654 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1658 name
= nl80211_ifname2phy(ifname
);
1665 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1667 name
= nl80211_ifname2phy(name
);
1671 strcpy(buf
, ifname
);
1680 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1681 struct iwinfo_rate_entry
*re
)
1683 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1684 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1685 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1686 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1688 if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1691 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1693 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1694 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1696 else if (ri
[NL80211_RATE_INFO_MCS
])
1699 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1702 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1704 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1706 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1708 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1710 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1711 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
1716 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
1717 re
->is_short_gi
= 1;
1719 re
->is_40mhz
= (re
->mhz
== 40);
1722 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
1724 struct nl80211_array_buf
*arr
= arg
;
1725 struct iwinfo_survey_entry
*e
= arr
->buf
;
1726 struct nlattr
**attr
= nl80211_parse(msg
);
1727 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
1730 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1731 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1732 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1733 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
1734 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
1735 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
1736 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
1737 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
1740 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
1741 attr
[NL80211_ATTR_SURVEY_INFO
],
1746 /* advance to end of array */
1748 memset(e
, 0, sizeof(*e
));
1750 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
1751 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
1753 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
1754 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
1756 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
1757 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
1759 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
1760 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
1762 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
1763 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
1765 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
1766 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
1768 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
1769 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
1776 static void plink_state_to_str(char *dst
, unsigned state
)
1779 case NL80211_PLINK_LISTEN
:
1780 strcpy(dst
, "LISTEN");
1782 case NL80211_PLINK_OPN_SNT
:
1783 strcpy(dst
, "OPN_SNT");
1785 case NL80211_PLINK_OPN_RCVD
:
1786 strcpy(dst
, "OPN_RCVD");
1788 case NL80211_PLINK_CNF_RCVD
:
1789 strcpy(dst
, "CNF_RCVD");
1791 case NL80211_PLINK_ESTAB
:
1792 strcpy(dst
, "ESTAB");
1794 case NL80211_PLINK_HOLDING
:
1795 strcpy(dst
, "HOLDING");
1797 case NL80211_PLINK_BLOCKED
:
1798 strcpy(dst
, "BLOCKED");
1801 strcpy(dst
, "UNKNOWN");
1806 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
1808 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
1811 case NL80211_MESH_POWER_ACTIVE
:
1812 strcpy(dst
, "ACTIVE");
1814 case NL80211_MESH_POWER_LIGHT_SLEEP
:
1815 strcpy(dst
, "LIGHT SLEEP");
1817 case NL80211_MESH_POWER_DEEP_SLEEP
:
1818 strcpy(dst
, "DEEP SLEEP");
1821 strcpy(dst
, "UNKNOWN");
1826 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
1828 struct nl80211_array_buf
*arr
= arg
;
1829 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
1830 struct nlattr
**attr
= nl80211_parse(msg
);
1831 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1832 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1833 struct nl80211_sta_flag_update
*sta_flags
;
1835 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1836 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1837 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1838 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1839 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
1840 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1841 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1842 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
1843 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1844 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1845 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
1846 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
1847 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
1848 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
1849 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
1850 [NL80211_STA_INFO_STA_FLAGS
] =
1851 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
1852 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
1854 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1855 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1856 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1857 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
1858 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
1859 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
1862 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1863 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1864 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1865 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1866 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1869 /* advance to end of array */
1871 memset(e
, 0, sizeof(*e
));
1873 if (attr
[NL80211_ATTR_MAC
])
1874 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
1876 if (attr
[NL80211_ATTR_STA_INFO
] &&
1877 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1878 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1880 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1881 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1883 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
1884 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
1886 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
1887 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
1889 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
1890 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
1892 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
1893 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
1895 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
1896 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
1898 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
1899 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1900 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
1901 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
1903 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
1904 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1905 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
1906 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
1908 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
1909 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
1911 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
1912 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
1914 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
1915 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
1917 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
1918 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
1920 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
1921 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
1923 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
1924 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
1926 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
1927 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
1930 if (sinfo
[NL80211_STA_INFO_LLID
])
1931 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
1933 if (sinfo
[NL80211_STA_INFO_PLID
])
1934 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
1936 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
1937 plink_state_to_str(e
->plink_state
,
1938 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
1940 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
1941 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
1942 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
1943 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
1944 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
1945 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
1948 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
1950 sta_flags
= (struct nl80211_sta_flag_update
*)
1951 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
1953 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
1954 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
1955 e
->is_authorized
= 1;
1957 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
1958 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
1959 e
->is_authenticated
= 1;
1961 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
1962 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
1963 e
->is_preamble_short
= 1;
1965 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
1966 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
1969 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
1970 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
1973 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
1974 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
1979 e
->noise
= 0; /* filled in by caller */
1985 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
1987 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
1990 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
1991 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
1993 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2000 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2005 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2006 struct iwinfo_assoclist_entry
*e
;
2008 if ((d
= opendir("/sys/class/net")) != NULL
)
2010 while ((de
= readdir(d
)) != NULL
)
2012 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2013 (!de
->d_name
[strlen(ifname
)] ||
2014 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2016 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2017 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2023 if (!nl80211_get_noise(ifname
, &noise
))
2024 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2027 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2034 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2037 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2039 struct nlattr
**attr
= nl80211_parse(msg
);
2040 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2041 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2042 struct nlattr
*band
, *freq
;
2044 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2045 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2046 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2047 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2048 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2049 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2050 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2053 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2056 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2058 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2059 nla_len(band
), NULL
);
2061 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2063 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2064 nla_data(freq
), nla_len(freq
), freq_policy
);
2066 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2067 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2069 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2070 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2072 *dbm_max
= (int)(0.01 * nla_get_u32(
2073 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2083 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2086 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2087 struct nl80211_msg_conveyor
*req
;
2088 struct iwinfo_txpwrlist_entry entry
;
2090 if (nl80211_get_channel(ifname
, &ch_cur
))
2093 /* initialize the value pointer with channel for callback */
2096 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2097 nl80211_get_txpwrlist_cb
, &dbm_max
);
2101 for (dbm_cur
= 0, dbm_cnt
= 0;
2103 dbm_cur
++, dbm_cnt
++)
2105 entry
.dbm
= dbm_cur
;
2106 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2108 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2111 entry
.dbm
= dbm_max
;
2112 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2114 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2117 *len
= dbm_cnt
* sizeof(entry
);
2124 static void nl80211_get_scancrypto(const char *spec
,
2125 struct iwinfo_crypto_entry
*c
)
2127 if (strstr(spec
, "WPA") || strstr(spec
, "WEP"))
2131 if (strstr(spec
, "WPA2-") && strstr(spec
, "WPA-"))
2134 else if (strstr(spec
, "WPA2"))
2137 else if (strstr(spec
, "WPA"))
2140 else if (strstr(spec
, "WEP"))
2141 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2144 if (strstr(spec
, "PSK"))
2145 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
2147 if (strstr(spec
, "802.1X") || strstr(spec
, "EAP"))
2148 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
2150 if (strstr(spec
, "WPA-NONE"))
2151 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
2154 if (strstr(spec
, "TKIP"))
2155 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
2157 if (strstr(spec
, "CCMP"))
2158 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
2160 if (strstr(spec
, "WEP-40"))
2161 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
2163 if (strstr(spec
, "WEP-104"))
2164 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
2166 c
->group_ciphers
= c
->pair_ciphers
;
2175 struct nl80211_scanlist
{
2176 struct iwinfo_scanlist_entry
*e
;
2181 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2182 struct iwinfo_scanlist_entry
*e
)
2184 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2185 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2186 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2189 while (ielen
>= 2 && ielen
>= ie
[1])
2194 case 114: /* Mesh ID */
2195 if (e
->ssid
[0] == 0) {
2196 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2197 memcpy(e
->ssid
, ie
+ 2, len
);
2203 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2204 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2207 case 221: /* Vendor */
2208 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2209 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2210 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2219 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2224 struct nl80211_scanlist
*sl
= arg
;
2225 struct nlattr
**tb
= nl80211_parse(msg
);
2226 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2228 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2229 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2230 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2231 [NL80211_BSS_BSSID
] = { 0 },
2232 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2233 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2234 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2235 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2236 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2237 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2238 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2239 [NL80211_BSS_BEACON_IES
] = { 0 },
2242 if (!tb
[NL80211_ATTR_BSS
] ||
2243 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2245 !bss
[NL80211_BSS_BSSID
])
2250 if (bss
[NL80211_BSS_CAPABILITY
])
2251 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2255 memset(sl
->e
, 0, sizeof(*sl
->e
));
2256 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2259 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2260 else if (caps
& (1<<0))
2261 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2263 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2266 sl
->e
->crypto
.enabled
= 1;
2268 if (bss
[NL80211_BSS_FREQUENCY
])
2269 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2270 bss
[NL80211_BSS_FREQUENCY
]));
2272 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2273 nl80211_get_scanlist_ie(bss
, sl
->e
);
2275 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2278 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2280 rssi
= sl
->e
->signal
- 0x100;
2284 else if (rssi
> -40)
2287 sl
->e
->quality
= (rssi
+ 110);
2288 sl
->e
->quality_max
= 70;
2291 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2293 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2294 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2303 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2305 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2307 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2310 if (nl80211_wait("nl80211", "scan",
2311 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2314 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2315 nl80211_get_scanlist_cb
, &sl
))
2318 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2326 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2329 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2330 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2336 if (len
+ 1 >= outlen
)
2346 out
[len
++] = '\n'; in
++;
2350 out
[len
++] = '\r'; in
++;
2354 out
[len
++] = '\t'; in
++;
2358 out
[len
++] = '\033'; in
++;
2362 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2363 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2385 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2387 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2388 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2389 struct sockaddr_un local
= { 0 };
2390 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2392 sock
= nl80211_wpactl_connect(ifname
, &local
);
2397 send(sock
, "ATTACH", 6, 0);
2398 send(sock
, "SCAN", 4, 0);
2401 * wait for scan results:
2402 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2403 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2404 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2405 * time to process the results, so try 72 + 2 + 1 times.
2407 for (tries
= 0; tries
< 75; tries
++)
2409 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2412 /* got an event notification */
2413 if (reply
[0] == '<')
2415 /* scan results are ready */
2416 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2418 /* send "SCAN_RESULTS" command */
2419 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2423 /* is another unrelated event, retry */
2427 /* got a failure reply */
2428 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2434 /* receive and parse scan results if the wait above didn't time out */
2435 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2437 /* received an event notification, receive again */
2438 if (reply
[0] == '<')
2441 nl80211_get_quality_max(ifname
, &qmax
);
2443 for (line
= strtok_r(reply
, "\n", &pos
);
2445 line
= strtok_r(NULL
, "\n", &pos
))
2447 /* skip header line */
2454 bssid
= strtok(line
, "\t");
2455 freq
= strtok(NULL
, "\t");
2456 signal
= strtok(NULL
, "\t");
2457 flags
= strtok(NULL
, "\t");
2458 ssid
= strtok(NULL
, "\n");
2460 if (!bssid
|| !freq
|| !signal
|| !flags
|| !ssid
)
2464 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2465 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2466 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2467 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2468 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2469 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2472 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2475 if (strstr(flags
, "[MESH]"))
2476 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2477 else if (strstr(flags
, "[IBSS]"))
2478 e
->mode
= IWINFO_OPMODE_ADHOC
;
2480 e
->mode
= IWINFO_OPMODE_MASTER
;
2483 e
->channel
= nl80211_freq2channel(atoi(freq
));
2486 rssi
= atoi(signal
);
2492 /* The cfg80211 wext compat layer assumes a signal range
2493 * of -110 dBm to -40 dBm, the quality value is derived
2494 * by adding 110 to the signal level */
2497 else if (rssi
> -40)
2500 e
->quality
= (rssi
+ 110);
2508 e
->quality_max
= qmax
;
2511 nl80211_get_scancrypto(flags
, &e
->crypto
);
2517 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2522 unlink(local
.sun_path
);
2524 return (count
>= 0) ? 0 : -1;
2527 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2534 /* Got a radioX pseudo interface, find some interface on it or create one */
2535 if (!strncmp(ifname
, "radio", 5))
2537 /* Reuse existing interface */
2538 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2540 return nl80211_get_scanlist(res
, buf
, len
);
2543 /* Need to spawn a temporary iface for scanning */
2544 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2546 rv
= nl80211_get_scanlist(res
, buf
, len
);
2552 /* WPA supplicant */
2553 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2558 /* station / ad-hoc / monitor scan */
2559 else if (!nl80211_get_mode(ifname
, &mode
) &&
2560 (mode
== IWINFO_OPMODE_ADHOC
||
2561 mode
== IWINFO_OPMODE_MASTER
||
2562 mode
== IWINFO_OPMODE_CLIENT
||
2563 mode
== IWINFO_OPMODE_MONITOR
) &&
2564 iwinfo_ifup(ifname
))
2566 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2572 /* Got a temp interface, don't create yet another one */
2573 if (!strncmp(ifname
, "tmp.", 4))
2575 if (!iwinfo_ifup(ifname
))
2578 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2579 iwinfo_ifdown(ifname
);
2583 /* Spawn a new scan interface */
2586 if (!(res
= nl80211_ifadd(ifname
)))
2591 /* if we can take the new interface up, the driver supports an
2592 * additional interface and there's no need to tear down the ap */
2593 if (iwinfo_ifup(res
))
2595 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2599 /* driver cannot create secondary interface, take down ap
2601 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2603 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2605 iwinfo_ifup(ifname
);
2606 nl80211_hostapd_hup(ifname
);
2617 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2619 int bands_remain
, freqs_remain
;
2621 struct nl80211_array_buf
*arr
= arg
;
2622 struct iwinfo_freqlist_entry
*e
;
2624 struct nlattr
**attr
= nl80211_parse(msg
);
2625 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2626 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2627 struct nlattr
*band
, *freq
;
2632 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2633 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2635 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2636 nla_data(band
), nla_len(band
), NULL
);
2638 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2639 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2641 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2642 nla_data(freq
), nla_len(freq
), NULL
);
2644 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2645 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2648 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2649 e
->channel
= nl80211_freq2channel(e
->mhz
);
2652 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2653 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2656 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2657 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2658 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2659 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2660 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2661 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2662 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2663 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2664 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2665 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2666 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2667 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2679 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2681 struct nl80211_msg_conveyor
*cv
;
2682 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2683 uint32_t features
= nl80211_get_protocol_features(ifname
);
2686 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
2687 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
2691 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
2692 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
2695 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
2705 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
2708 struct nlattr
**attr
= nl80211_parse(msg
);
2710 if (attr
[NL80211_ATTR_REG_ALPHA2
])
2711 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
2718 static int nl80211_get_country(const char *ifname
, char *buf
)
2720 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
2721 nl80211_get_country_cb
, buf
))
2727 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
2730 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
2731 const struct iwinfo_iso3166_label
*l
;
2733 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
2735 e
->iso3166
= l
->iso3166
;
2736 e
->ccode
[0] = (l
->iso3166
/ 256);
2737 e
->ccode
[1] = (l
->iso3166
% 256);
2741 *len
= (count
* sizeof(struct iwinfo_country_entry
));
2746 struct nl80211_modes
2753 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
2755 struct nl80211_modes
*m
= arg
;
2756 int bands_remain
, freqs_remain
;
2758 uint32_t vht_caps
= 0;
2759 struct nlattr
**attr
= nl80211_parse(msg
);
2760 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2761 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2762 struct nlattr
*band
, *freq
;
2764 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
2766 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2768 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2769 nla_data(band
), nla_len(band
), NULL
);
2771 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
2772 caps
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
2774 /* Treat any nonzero capability as 11n */
2777 m
->hw
|= IWINFO_80211_N
;
2778 m
->ht
|= IWINFO_HTMODE_HT20
;
2780 if (caps
& (1 << 1))
2781 m
->ht
|= IWINFO_HTMODE_HT40
;
2784 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
2787 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2788 nla_data(freq
), nla_len(freq
), NULL
);
2790 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
2793 if (nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]) < 2485)
2795 m
->hw
|= IWINFO_80211_B
;
2796 m
->hw
|= IWINFO_80211_G
;
2798 else if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
2800 vht_caps
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
2802 /* Treat any nonzero capability as 11ac */
2805 m
->hw
|= IWINFO_80211_AC
;
2806 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
2808 switch ((vht_caps
>> 2) & 3)
2811 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
2815 m
->ht
|= IWINFO_HTMODE_VHT160
;
2819 else if (nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]) >= 56160)
2821 m
->hw
|= IWINFO_80211_AD
;
2823 else if (!(m
->hw
& IWINFO_80211_AC
))
2825 m
->hw
|= IWINFO_80211_A
;
2836 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
2838 struct nl80211_modes m
= { 0 };
2840 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2841 nl80211_get_modelist_cb
, &m
))
2855 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
2857 struct nl80211_modes m
= { 0 };
2859 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2860 nl80211_get_modelist_cb
, &m
))
2875 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
2877 struct nlattr
**attr
= nl80211_parse(msg
);
2878 struct nlattr
*comb
;
2880 int comb_rem
, limit_rem
, mode_rem
;
2883 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
2886 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
2888 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
2889 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
2890 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
2892 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
2893 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
2894 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
2895 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
2897 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
2898 struct nlattr
*limit
;
2900 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
2902 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
2905 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
2907 struct nlattr
*mode
;
2909 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
2911 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
2912 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
2915 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
2918 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
2919 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
2928 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
2930 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2931 nl80211_get_ifcomb_cb
, buf
))
2937 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
2939 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
2940 char *phy
, num
[8], path
[PATH_MAX
];
2943 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
2944 { "vendor", &id
->vendor_id
},
2945 { "device", &id
->device_id
},
2946 { "subsystem_vendor", &id
->subsystem_vendor_id
},
2947 { "subsystem_device", &id
->subsystem_device_id
}
2950 memset(id
, 0, sizeof(*id
));
2952 /* Try to determine the phy name from the given interface */
2953 phy
= nl80211_ifname2phy(ifname
);
2955 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
2957 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
2958 phy
? "ieee80211" : "net",
2959 phy
? phy
: ifname
, lookup
[i
].path
);
2961 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
2962 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
2965 /* Failed to obtain hardware IDs, search board config */
2966 if (id
->vendor_id
== 0 || id
->device_id
== 0)
2967 return iwinfo_hardware_id_from_mtd(id
);
2972 static const struct iwinfo_hardware_entry
*
2973 nl80211_get_hardware_entry(const char *ifname
)
2975 struct iwinfo_hardware_id id
;
2977 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
2980 return iwinfo_hardware(&id
);
2983 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
2985 const struct iwinfo_hardware_entry
*hw
;
2987 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
2988 sprintf(buf
, "Generic MAC80211");
2990 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
2995 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
2997 const struct iwinfo_hardware_entry
*hw
;
2999 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3002 *buf
= hw
->txpower_offset
;
3006 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3008 const struct iwinfo_hardware_entry
*hw
;
3010 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3013 *buf
= hw
->frequency_offset
;
3017 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3021 if ((idx
= nl80211_phy_idx_from_uci(section
)) < 0)
3024 sprintf(buf
, "phy%d", idx
);
3028 const struct iwinfo_ops nl80211_ops
= {
3030 .probe
= nl80211_probe
,
3031 .channel
= nl80211_get_channel
,
3032 .frequency
= nl80211_get_frequency
,
3033 .frequency_offset
= nl80211_get_frequency_offset
,
3034 .txpower
= nl80211_get_txpower
,
3035 .txpower_offset
= nl80211_get_txpower_offset
,
3036 .bitrate
= nl80211_get_bitrate
,
3037 .signal
= nl80211_get_signal
,
3038 .noise
= nl80211_get_noise
,
3039 .quality
= nl80211_get_quality
,
3040 .quality_max
= nl80211_get_quality_max
,
3041 .mbssid_support
= nl80211_get_mbssid_support
,
3042 .hwmodelist
= nl80211_get_hwmodelist
,
3043 .htmodelist
= nl80211_get_htmodelist
,
3044 .mode
= nl80211_get_mode
,
3045 .ssid
= nl80211_get_ssid
,
3046 .bssid
= nl80211_get_bssid
,
3047 .country
= nl80211_get_country
,
3048 .hardware_id
= nl80211_get_hardware_id
,
3049 .hardware_name
= nl80211_get_hardware_name
,
3050 .encryption
= nl80211_get_encryption
,
3051 .phyname
= nl80211_get_phyname
,
3052 .assoclist
= nl80211_get_assoclist
,
3053 .txpwrlist
= nl80211_get_txpwrlist
,
3054 .scanlist
= nl80211_get_scanlist
,
3055 .freqlist
= nl80211_get_freqlist
,
3056 .countrylist
= nl80211_get_countrylist
,
3057 .survey
= nl80211_get_survey
,
3058 .lookup_phy
= nl80211_lookup_phyname
,
3059 .close
= nl80211_close