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
;
309 path_len
= strlen(path
);
313 d
= opendir("/sys/class/ieee80211");
317 while ((e
= readdir(d
)) != NULL
) {
318 cur_path
= nl80211_phy_path_str(e
->d_name
);
322 cur_path_len
= strlen(cur_path
);
323 if (cur_path_len
< path_len
)
326 if (strcmp(cur_path
+ cur_path_len
- path_len
, path
) != 0)
329 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
330 idx
= nl80211_readint(buf
);
341 static int nl80211_phy_idx_from_macaddr(const char *opt
)
350 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
351 if (glob(buf
, 0, NULL
, &gl
))
354 for (i
= 0; i
< gl
.gl_pathc
; i
++)
356 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
357 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
360 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
363 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
364 if ((idx
= nl80211_readint(buf
)) > -1)
373 static int nl80211_phy_idx_from_phy(const char *opt
)
380 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
381 return nl80211_readint(buf
);
384 static int nl80211_phy_idx_from_uci(const char *name
)
386 struct uci_section
*s
;
390 s
= iwinfo_uci_get_radio(name
, "mac80211");
394 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
395 idx
= nl80211_phy_idx_from_path(opt
);
399 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
400 idx
= nl80211_phy_idx_from_macaddr(opt
);
404 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
405 idx
= nl80211_phy_idx_from_phy(opt
);
412 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
415 int ifidx
= -1, phyidx
= -1;
416 struct nl80211_msg_conveyor
*cv
;
421 if (nl80211_init() < 0)
424 if (!strncmp(ifname
, "phy", 3))
425 phyidx
= atoi(&ifname
[3]);
426 else if (!strncmp(ifname
, "radio", 5))
427 phyidx
= nl80211_phy_idx_from_uci(ifname
);
429 if (!strncmp(ifname
, "mon.", 4))
430 ifidx
= if_nametoindex(&ifname
[4]);
432 ifidx
= if_nametoindex(ifname
);
434 /* Valid ifidx must be greater than 0 */
435 if ((ifidx
<= 0) && (phyidx
< 0))
438 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
443 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
444 else if (phyidx
> -1)
445 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
454 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
455 int (*cb_func
)(struct nl_msg
*, void *),
458 static struct nl80211_msg_conveyor rcv
;
462 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
464 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
466 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
473 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
474 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
475 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
478 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
485 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
486 int (*cb_func
)(struct nl_msg
*, void *),
489 struct nl80211_msg_conveyor
*cv
;
491 cv
= nl80211_msg(ifname
, cmd
, flags
);
496 return nl80211_send(cv
, cb_func
, cb_arg
);
499 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
501 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
502 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
504 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
505 genlmsg_attrlen(gnlh
, 0), NULL
);
510 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
512 uint32_t *features
= arg
;
513 struct nlattr
**attr
= nl80211_parse(msg
);
515 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
516 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
521 static int nl80211_get_protocol_features(const char *ifname
)
523 struct nl80211_msg_conveyor
*req
;
524 uint32_t features
= 0;
526 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
528 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
535 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
537 struct nl80211_group_conveyor
*cv
= arg
;
539 struct nlattr
**attr
= nl80211_parse(msg
);
540 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
544 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
547 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
549 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
550 nla_data(mgrp
), nla_len(mgrp
), NULL
);
552 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
553 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
554 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
555 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
557 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
565 static int nl80211_subscribe(const char *family
, const char *group
)
567 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
568 struct nl80211_msg_conveyor
*req
;
571 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
574 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
575 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
580 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
590 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
592 struct nl80211_event_conveyor
*cv
= arg
;
593 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
595 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
596 cv
->recv
= gnlh
->cmd
;
601 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
606 static int __nl80211_wait(const char *family
, const char *group
, ...)
608 struct nl80211_event_conveyor cv
= { };
614 if (nl80211_subscribe(family
, group
))
617 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
622 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
623 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
624 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
628 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
629 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
633 while (!cv
.recv
&& !err
)
634 nl_recvmsgs(nls
->nl_sock
, cb
);
641 #define nl80211_wait(family, group, ...) \
642 __nl80211_wait(family, group, __VA_ARGS__, 0)
645 static int nl80211_freq2channel(int freq
)
649 else if (freq
< 2484)
650 return (freq
- 2407) / 5;
651 else if (freq
>= 4910 && freq
<= 4980)
652 return (freq
- 4000) / 5;
653 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
654 return (freq
- 56160) / 2160;
656 return (freq
- 5000) / 5;
659 static int nl80211_channel2freq(int channel
, const char *band
)
661 if (!band
|| band
[0] != 'a')
665 else if (channel
< 14)
666 return (channel
* 5) + 2407;
668 else if ( strcmp(band
, "ad") == 0)
670 return 56160 + 2160 * channel
;
674 if (channel
>= 182 && channel
<= 196)
675 return (channel
* 5) + 4000;
677 return (channel
* 5) + 5000;
683 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
686 struct nlattr
**attr
= nl80211_parse(msg
);
688 if (attr
[NL80211_ATTR_WIPHY_NAME
])
689 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
690 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
697 static char * nl80211_ifname2phy(const char *ifname
)
699 static char phy
[32] = { 0 };
701 memset(phy
, 0, sizeof(phy
));
703 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
704 nl80211_ifname2phy_cb
, phy
);
706 return phy
[0] ? phy
: NULL
;
709 static char * nl80211_phy2ifname(const char *ifname
)
711 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
713 static char nif
[IFNAMSIZ
] = { 0 };
718 /* Only accept phy name of the form phy%d or radio%d */
721 else if (!strncmp(ifname
, "phy", 3))
722 phyidx
= atoi(&ifname
[3]);
723 else if (!strncmp(ifname
, "radio", 5))
724 phyidx
= nl80211_phy_idx_from_uci(ifname
);
728 memset(nif
, 0, sizeof(nif
));
732 if ((d
= opendir("/sys/class/net")) != NULL
)
734 while ((e
= readdir(d
)) != NULL
)
736 snprintf(buffer
, sizeof(buffer
),
737 "/sys/class/net/%s/phy80211/index", e
->d_name
);
739 if (nl80211_readint(buffer
) == phyidx
)
741 snprintf(buffer
, sizeof(buffer
),
742 "/sys/class/net/%s/ifindex", e
->d_name
);
744 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
745 ((ifidx
< 0) || (cifidx
< ifidx
)))
748 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
757 return nif
[0] ? nif
: NULL
;
760 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
763 struct nlattr
**tb
= nl80211_parse(msg
);
764 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
765 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
766 IWINFO_OPMODE_ADHOC
, /* IBSS */
767 IWINFO_OPMODE_CLIENT
, /* managed */
768 IWINFO_OPMODE_MASTER
, /* AP */
769 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
770 IWINFO_OPMODE_WDS
, /* WDS */
771 IWINFO_OPMODE_MONITOR
, /* monitor */
772 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
773 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
774 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
777 if (tb
[NL80211_ATTR_IFTYPE
])
778 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
784 static int nl80211_get_mode(const char *ifname
, int *buf
)
788 *buf
= IWINFO_OPMODE_UNKNOWN
;
790 res
= nl80211_phy2ifname(ifname
);
792 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
793 nl80211_get_mode_cb
, buf
);
795 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
798 static int __nl80211_hostapd_query(const char *ifname
, ...)
801 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
802 int len
, mode
, found
= 0, match
= 1;
805 if (nl80211_get_mode(ifname
, &mode
))
808 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
811 phy
= nl80211_ifname2phy(ifname
);
816 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
817 fp
= fopen(buf
, "r");
822 va_start(ap
, ifname
);
824 /* clear all destination buffers */
827 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
829 dest
= va_arg(ap_cur
, char *);
830 len
= va_arg(ap_cur
, int);
832 memset(dest
, 0, len
);
837 /* iterate applicable lines and copy found values into dest buffers */
838 while (fgets(buf
, sizeof(buf
), fp
))
840 key
= strtok(buf
, " =\t\n");
841 val
= strtok(NULL
, "\n");
843 if (!key
|| !val
|| !*key
|| *key
== '#')
846 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
847 match
= !strcmp(ifname
, val
);
854 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
856 dest
= va_arg(ap_cur
, char *);
857 len
= va_arg(ap_cur
, int);
859 if (!strcmp(search
, key
))
861 strncpy(dest
, val
, len
- 1);
877 #define nl80211_hostapd_query(ifname, ...) \
878 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
881 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
884 struct timeval tv
= { 0, 256000 };
889 memset(buf
, 0, blen
);
891 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
894 if (!FD_ISSET(sock
, &rfds
))
897 return recv(sock
, buf
, blen
- 1, 0);
900 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
902 struct sockaddr_un remote
= { 0 };
903 size_t remote_length
, local_length
;
905 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
909 remote
.sun_family
= AF_UNIX
;
910 remote_length
= sizeof(remote
.sun_family
) +
911 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
914 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
920 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
922 remote_length
= sizeof(remote
.sun_family
) +
923 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
925 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
932 local
->sun_family
= AF_UNIX
;
933 local_length
= sizeof(local
->sun_family
) +
934 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
936 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
945 static int __nl80211_wpactl_query(const char *ifname
, ...)
948 struct sockaddr_un local
= { 0 };
949 int len
, mode
, found
= 0, sock
= -1;
950 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
952 if (nl80211_get_mode(ifname
, &mode
))
955 if (mode
!= IWINFO_OPMODE_CLIENT
&&
956 mode
!= IWINFO_OPMODE_ADHOC
&&
957 mode
!= IWINFO_OPMODE_MESHPOINT
)
960 sock
= nl80211_wpactl_connect(ifname
, &local
);
965 va_start(ap
, ifname
);
967 /* clear all destination buffers */
970 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
972 dest
= va_arg(ap_cur
, char *);
973 len
= va_arg(ap_cur
, int);
975 memset(dest
, 0, len
);
980 send(sock
, "STATUS", 6, 0);
984 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
990 for (line
= strtok_r(buf
, "\n", &pos
);
992 line
= strtok_r(NULL
, "\n", &pos
))
994 key
= strtok(line
, "=");
995 val
= strtok(NULL
, "\n");
1000 va_copy(ap_cur
, ap
);
1002 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
1004 dest
= va_arg(ap_cur
, char *);
1005 len
= va_arg(ap_cur
, int);
1007 if (!strcmp(search
, key
))
1009 strncpy(dest
, val
, len
- 1);
1024 unlink(local
.sun_path
);
1029 #define nl80211_wpactl_query(ifname, ...) \
1030 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1033 static char * nl80211_ifadd(const char *ifname
)
1035 char path
[PATH_MAX
];
1036 static char nif
[IFNAMSIZ
] = { 0 };
1037 struct nl80211_msg_conveyor
*req
;
1040 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
1043 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
1045 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
1046 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
1048 nl80211_send(req
, NULL
, NULL
);
1050 snprintf(path
, sizeof(path
) - 1,
1051 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
1053 if ((sysfs
= fopen(path
, "w")) != NULL
)
1055 fwrite("0\n", 1, 2, sysfs
);
1068 static void nl80211_ifdel(const char *ifname
)
1070 struct nl80211_msg_conveyor
*req
;
1073 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1076 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1078 nl80211_send(req
, NULL
, NULL
);
1086 static void nl80211_hostapd_hup(const char *ifname
)
1090 char *phy
= nl80211_ifname2phy(ifname
);
1094 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1095 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1097 if (read(fd
, buf
, sizeof(buf
)) > 0)
1109 static int nl80211_probe(const char *ifname
)
1111 return !!nl80211_ifname2phy(ifname
);
1114 struct nl80211_ssid_bssid
{
1115 unsigned char *ssid
;
1116 unsigned char bssid
[7];
1119 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1121 struct nl80211_ssid_bssid
*sb
= arg
;
1122 struct nlattr
**tb
= nl80211_parse(msg
);
1124 if (tb
[NL80211_ATTR_MAC
]) {
1126 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1127 sizeof(sb
->bssid
) - 1);
1133 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1137 struct nl80211_ssid_bssid
*sb
= arg
;
1138 struct nlattr
**tb
= nl80211_parse(msg
);
1139 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1141 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1142 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1143 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1146 if (!tb
[NL80211_ATTR_BSS
] ||
1147 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1149 !bss
[NL80211_BSS_BSSID
] ||
1150 !bss
[NL80211_BSS_STATUS
] ||
1151 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1156 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1158 case NL80211_BSS_STATUS_ASSOCIATED
:
1159 case NL80211_BSS_STATUS_AUTHENTICATED
:
1160 case NL80211_BSS_STATUS_IBSS_JOINED
:
1164 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1165 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1167 while (ielen
>= 2 && ielen
>= ie
[1])
1171 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1182 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1191 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1194 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1196 /* try to find ssid from scan dump results */
1197 res
= nl80211_phy2ifname(ifname
);
1200 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1201 nl80211_get_ssid_bssid_cb
, &sb
);
1203 /* failed, try to find from hostapd info */
1204 if (sb
.ssid
[0] == 0)
1205 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1206 IWINFO_ESSID_MAX_SIZE
+ 1);
1208 /* failed, try to obtain Mesh ID */
1209 if (sb
.ssid
[0] == 0)
1210 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1211 sb
.ssid
, IWINFO_ESSID_MAX_SIZE
+ 1);
1213 return (sb
.ssid
[0] == 0) ? -1 : 0;
1216 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1218 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1219 struct nl80211_ssid_bssid sb
= { };
1221 res
= nl80211_phy2ifname(ifname
);
1223 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1224 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1225 nl80211_get_macaddr_cb
, &sb
);
1227 /* failed, try to find bssid from scan dump results */
1228 if (sb
.bssid
[0] == 0)
1229 nl80211_request(res
? res
: ifname
,
1230 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1231 nl80211_get_ssid_bssid_cb
, &sb
);
1233 /* failed, try to find mac from hostapd info */
1234 if ((sb
.bssid
[0] == 0) &&
1235 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1238 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1239 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1240 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1241 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1242 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1243 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1248 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1249 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1250 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1259 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1262 struct nlattr
**attr
= nl80211_parse(msg
);
1263 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1265 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1266 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1267 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1270 if (attr
[NL80211_ATTR_BSS
] &&
1271 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1272 attr
[NL80211_ATTR_BSS
], bss_policy
))
1274 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1275 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1281 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1284 struct nlattr
**tb
= nl80211_parse(msg
);
1286 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1287 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1292 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1294 char *res
, channel
[4], hwmode
[3];
1296 /* try to find frequency from interface info */
1297 res
= nl80211_phy2ifname(ifname
);
1300 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1301 nl80211_get_frequency_info_cb
, buf
);
1303 /* failed, try to find frequency from hostapd info */
1305 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1306 "channel", channel
, sizeof(channel
)) == 2)
1308 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1311 /* failed, try to find frequency from scan results */
1314 res
= nl80211_phy2ifname(ifname
);
1316 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1317 nl80211_get_frequency_scan_cb
, buf
);
1320 return (*buf
== 0) ? -1 : 0;
1323 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1326 struct nlattr
**tb
= nl80211_parse(msg
);
1328 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1329 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1334 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1338 /* try to find frequency from interface info */
1339 res
= nl80211_phy2ifname(ifname
);
1342 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1343 nl80211_get_center_freq1_cb
, buf
);
1345 return (*buf
== 0) ? -1 : 0;
1348 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1351 struct nlattr
**tb
= nl80211_parse(msg
);
1353 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1354 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1359 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1363 /* try to find frequency from interface info */
1364 res
= nl80211_phy2ifname(ifname
);
1367 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1368 nl80211_get_center_freq2_cb
, buf
);
1370 return (*buf
== 0) ? -1 : 0;
1373 static int nl80211_get_channel(const char *ifname
, int *buf
)
1375 if (!nl80211_get_frequency(ifname
, buf
))
1377 *buf
= nl80211_freq2channel(*buf
);
1384 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1386 if (!nl80211_get_center_freq1(ifname
, buf
))
1388 *buf
= nl80211_freq2channel(*buf
);
1395 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1397 if (!nl80211_get_center_freq2(ifname
, buf
))
1399 *buf
= nl80211_freq2channel(*buf
);
1406 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1409 struct nlattr
**tb
= nl80211_parse(msg
);
1411 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1412 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1417 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1421 res
= nl80211_phy2ifname(ifname
);
1424 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1425 nl80211_get_txpower_cb
, buf
))
1432 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1436 struct nl80211_rssi_rate
*rr
= arg
;
1437 struct nlattr
**attr
= nl80211_parse(msg
);
1438 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1439 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1441 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1442 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1443 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1444 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1445 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1446 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1447 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1448 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1449 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1450 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1451 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1454 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1455 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1456 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1457 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1458 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1461 if (attr
[NL80211_ATTR_STA_INFO
])
1463 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1464 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1466 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1468 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1469 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1473 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1475 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1476 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1479 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1481 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1482 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1493 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1498 memset(r
, 0, sizeof(*r
));
1500 if ((d
= opendir("/sys/class/net")) != NULL
)
1502 while ((de
= readdir(d
)) != NULL
)
1504 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1505 (!de
->d_name
[strlen(ifname
)] ||
1506 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1508 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1509 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1517 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1519 struct nl80211_rssi_rate rr
;
1521 nl80211_fill_signal(ifname
, &rr
);
1523 if (rr
.rate_samples
)
1525 *buf
= (rr
.rate
* 100);
1532 static int nl80211_get_signal(const char *ifname
, int *buf
)
1534 struct nl80211_rssi_rate rr
;
1536 nl80211_fill_signal(ifname
, &rr
);
1538 if (rr
.rssi_samples
)
1547 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1549 int8_t *noise
= arg
;
1550 struct nlattr
**tb
= nl80211_parse(msg
);
1551 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1553 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1554 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1555 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1558 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1561 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1562 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1565 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1568 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1569 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1575 static int nl80211_get_noise(const char *ifname
, int *buf
)
1579 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1580 nl80211_get_noise_cb
, &noise
))
1591 static int nl80211_get_quality(const char *ifname
, int *buf
)
1595 if (!nl80211_get_signal(ifname
, &signal
))
1597 /* A positive signal level is usually just a quality
1598 * value, pass through as-is */
1604 /* The cfg80211 wext compat layer assumes a signal range
1605 * of -110 dBm to -40 dBm, the quality value is derived
1606 * by adding 110 to the signal level */
1611 else if (signal
> -40)
1614 *buf
= (signal
+ 110);
1623 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1625 /* The cfg80211 wext compat layer assumes a maximum
1632 static int nl80211_check_wepkey(const char *key
)
1636 switch (strlen(key
))
1640 return IWINFO_CIPHER_WEP40
;
1644 return IWINFO_CIPHER_WEP104
;
1655 } wpa_key_mgmt_strings
[] = {
1656 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1657 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1658 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1659 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1660 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1661 { "NONE", 0, IWINFO_KMGMT_NONE
},
1662 { "None", 0, IWINFO_KMGMT_NONE
},
1663 { "PSK", 0, IWINFO_KMGMT_PSK
},
1664 { "EAP", 0, IWINFO_KMGMT_8021x
},
1665 { "SAE", 4, IWINFO_KMGMT_SAE
},
1666 { "OWE", 4, IWINFO_KMGMT_OWE
}
1669 static void parse_wpa_suites(const char *str
, int defversion
,
1670 uint8_t *versions
, uint8_t *suites
)
1674 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1680 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1682 m
= wpa_key_mgmt_strings
[i
].match
;
1685 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1687 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1688 version
= wpa_key_mgmt_strings
[i
].version
;
1690 version
= defversion
;
1692 *versions
|= version
;
1693 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1701 q
+= strcspn(q
, sep
);
1703 p
= q
+ strspn(q
, sep
);
1710 } wpa_cipher_strings
[] = {
1711 { "WEP-104", IWINFO_CIPHER_WEP104
},
1712 { "WEP-40", IWINFO_CIPHER_WEP40
},
1713 { "NONE", IWINFO_CIPHER_NONE
},
1714 { "TKIP", IWINFO_CIPHER_TKIP
},
1715 { "CCMP", IWINFO_CIPHER_CCMP
},
1716 { "GCMP", IWINFO_CIPHER_GCMP
}
1719 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1723 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1729 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1731 m
= wpa_cipher_strings
[i
].match
;
1734 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1736 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1744 q
+= strcspn(q
, sep
);
1746 p
= q
+ strspn(q
, sep
);
1750 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1754 uint8_t wpa_version
= 0;
1755 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1756 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1759 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1761 /* WPA supplicant */
1762 if (nl80211_wpactl_query(ifname
,
1763 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1764 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1765 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1766 "mode", mode
, sizeof(mode
)))
1769 if (!strcmp(wpa_key_mgmt
, "NONE"))
1771 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1772 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1774 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1776 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1777 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1780 c
->pair_ciphers
= 0;
1781 c
->group_ciphers
= 0;
1786 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1790 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1791 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1792 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1798 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1799 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1803 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1808 else if (!strncmp(p
, "WPA-", 4))
1814 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1816 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1823 else if (nl80211_hostapd_query(ifname
,
1824 "wpa", wpa
, sizeof(wpa
),
1825 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1826 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1827 "auth_algs", auth_algs
, sizeof(auth_algs
),
1828 "wep_key0", wep_key0
, sizeof(wep_key0
),
1829 "wep_key1", wep_key1
, sizeof(wep_key1
),
1830 "wep_key2", wep_key2
, sizeof(wep_key2
),
1831 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1835 if (wpa_key_mgmt
[0])
1837 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1839 if (!strncmp(p
, "WPA-", 4))
1842 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1845 c
->enabled
= c
->wpa_version
? 1 : 0;
1848 if (wpa_pairwise
[0])
1849 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1853 switch (atoi(auth_algs
))
1856 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1860 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1864 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1865 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1869 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1870 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1871 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1872 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1874 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1877 c
->group_ciphers
= c
->pair_ciphers
;
1882 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1883 else if (!nl80211_get_mode(ifname
, &opmode
) &&
1884 (opmode
== IWINFO_OPMODE_ADHOC
||
1885 opmode
== IWINFO_OPMODE_MESHPOINT
))
1896 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1900 name
= nl80211_ifname2phy(ifname
);
1907 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1909 name
= nl80211_ifname2phy(name
);
1913 strcpy(buf
, ifname
);
1922 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1923 struct iwinfo_rate_entry
*re
)
1925 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1926 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1927 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1928 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1930 if (ri
[NL80211_RATE_INFO_HE_MCS
])
1933 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_MCS
]);
1935 if (ri
[NL80211_RATE_INFO_HE_NSS
])
1936 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_NSS
]);
1937 if (ri
[NL80211_RATE_INFO_HE_GI
])
1938 re
->he_gi
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_GI
]);
1939 if (ri
[NL80211_RATE_INFO_HE_DCM
])
1940 re
->he_dcm
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_DCM
]);
1942 else if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1945 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1947 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1948 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1950 else if (ri
[NL80211_RATE_INFO_MCS
])
1953 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1956 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1958 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1960 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1962 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1964 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1965 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
1970 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
1971 re
->is_short_gi
= 1;
1973 re
->is_40mhz
= (re
->mhz
== 40);
1976 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
1978 struct nl80211_array_buf
*arr
= arg
;
1979 struct iwinfo_survey_entry
*e
= arr
->buf
;
1980 struct nlattr
**attr
= nl80211_parse(msg
);
1981 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
1984 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1985 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1986 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1987 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
1988 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
1989 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
1990 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
1991 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
1994 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
1995 attr
[NL80211_ATTR_SURVEY_INFO
],
2000 /* advance to end of array */
2002 memset(e
, 0, sizeof(*e
));
2004 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
2005 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
2007 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
2008 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
2010 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
2011 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
2013 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
2014 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
2016 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
2017 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
2019 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
2020 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
2022 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
2023 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
2030 static void plink_state_to_str(char *dst
, unsigned state
)
2033 case NL80211_PLINK_LISTEN
:
2034 strcpy(dst
, "LISTEN");
2036 case NL80211_PLINK_OPN_SNT
:
2037 strcpy(dst
, "OPN_SNT");
2039 case NL80211_PLINK_OPN_RCVD
:
2040 strcpy(dst
, "OPN_RCVD");
2042 case NL80211_PLINK_CNF_RCVD
:
2043 strcpy(dst
, "CNF_RCVD");
2045 case NL80211_PLINK_ESTAB
:
2046 strcpy(dst
, "ESTAB");
2048 case NL80211_PLINK_HOLDING
:
2049 strcpy(dst
, "HOLDING");
2051 case NL80211_PLINK_BLOCKED
:
2052 strcpy(dst
, "BLOCKED");
2055 strcpy(dst
, "UNKNOWN");
2060 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
2062 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
2065 case NL80211_MESH_POWER_ACTIVE
:
2066 strcpy(dst
, "ACTIVE");
2068 case NL80211_MESH_POWER_LIGHT_SLEEP
:
2069 strcpy(dst
, "LIGHT SLEEP");
2071 case NL80211_MESH_POWER_DEEP_SLEEP
:
2072 strcpy(dst
, "DEEP SLEEP");
2075 strcpy(dst
, "UNKNOWN");
2080 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2082 struct nl80211_array_buf
*arr
= arg
;
2083 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2084 struct nlattr
**attr
= nl80211_parse(msg
);
2085 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2086 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2087 struct nl80211_sta_flag_update
*sta_flags
;
2089 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2090 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2091 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2092 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2093 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2094 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2095 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2096 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2097 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2098 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2099 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2100 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2101 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2102 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2103 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2104 [NL80211_STA_INFO_STA_FLAGS
] =
2105 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2106 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2108 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2109 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2110 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2111 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2112 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2113 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2116 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2117 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2118 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2119 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2120 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2123 /* advance to end of array */
2125 memset(e
, 0, sizeof(*e
));
2127 if (attr
[NL80211_ATTR_MAC
])
2128 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2130 if (attr
[NL80211_ATTR_STA_INFO
] &&
2131 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2132 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2134 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2135 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2137 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2138 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2140 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2141 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2143 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2144 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2146 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2147 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2149 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2150 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2152 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2153 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2154 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2155 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2157 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2158 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2159 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2160 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2162 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2163 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2165 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2166 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2168 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2169 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2171 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2172 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2174 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2175 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2177 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2178 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2180 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2181 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2184 if (sinfo
[NL80211_STA_INFO_LLID
])
2185 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2187 if (sinfo
[NL80211_STA_INFO_PLID
])
2188 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2190 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2191 plink_state_to_str(e
->plink_state
,
2192 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2194 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2195 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2196 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2197 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2198 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2199 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2202 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2204 sta_flags
= (struct nl80211_sta_flag_update
*)
2205 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2207 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2208 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2209 e
->is_authorized
= 1;
2211 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2212 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2213 e
->is_authenticated
= 1;
2215 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2216 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2217 e
->is_preamble_short
= 1;
2219 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2220 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2223 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2224 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2227 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2228 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2233 e
->noise
= 0; /* filled in by caller */
2239 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2241 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2244 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2245 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2247 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2254 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2259 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2260 struct iwinfo_assoclist_entry
*e
;
2262 if ((d
= opendir("/sys/class/net")) != NULL
)
2264 while ((de
= readdir(d
)) != NULL
)
2266 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2267 (!de
->d_name
[strlen(ifname
)] ||
2268 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2270 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2271 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2277 if (!nl80211_get_noise(ifname
, &noise
))
2278 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2281 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2288 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2291 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2293 struct nlattr
**attr
= nl80211_parse(msg
);
2294 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2295 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2296 struct nlattr
*band
, *freq
;
2298 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2299 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2300 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2301 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2302 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2303 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2304 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2307 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2310 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2312 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2313 nla_len(band
), NULL
);
2315 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2317 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2318 nla_data(freq
), nla_len(freq
), freq_policy
);
2320 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2321 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2323 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2324 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2326 *dbm_max
= (int)(0.01 * nla_get_u32(
2327 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2337 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2340 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2341 struct nl80211_msg_conveyor
*req
;
2342 struct iwinfo_txpwrlist_entry entry
;
2344 if (nl80211_get_channel(ifname
, &ch_cur
))
2347 /* initialize the value pointer with channel for callback */
2350 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2351 nl80211_get_txpwrlist_cb
, &dbm_max
);
2355 for (dbm_cur
= 0, dbm_cnt
= 0;
2357 dbm_cur
++, dbm_cnt
++)
2359 entry
.dbm
= dbm_cur
;
2360 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2362 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2365 entry
.dbm
= dbm_max
;
2366 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2368 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2371 *len
= dbm_cnt
* sizeof(entry
);
2378 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2380 int wpa_version
= 0;
2381 char *p
, *q
, *proto
, *suites
;
2385 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2386 if (!strcmp(p
, "WEP")) {
2388 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2389 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2390 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2394 proto
= strtok(p
, "-");
2395 suites
= strtok(NULL
, "]");
2397 if (!proto
|| !suites
)
2400 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2402 else if (!strcmp(proto
, "WPA"))
2409 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2410 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2415 struct nl80211_scanlist
{
2416 struct iwinfo_scanlist_entry
*e
;
2421 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2422 struct iwinfo_scanlist_entry
*e
)
2424 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2425 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2426 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2429 while (ielen
>= 2 && ielen
>= ie
[1])
2434 case 114: /* Mesh ID */
2435 if (e
->ssid
[0] == 0) {
2436 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2437 memcpy(e
->ssid
, ie
+ 2, len
);
2443 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2444 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2447 case 221: /* Vendor */
2448 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2449 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2450 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2452 case 61: /* HT oeration */
2454 e
->ht_chan_info
.primary_chan
= ie
[2];
2455 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2456 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2459 case 192: /* VHT operation */
2461 e
->vht_chan_info
.chan_width
= ie
[2];
2462 e
->vht_chan_info
.center_chan_1
= ie
[3];
2463 e
->vht_chan_info
.center_chan_2
= ie
[4];
2473 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2478 struct nl80211_scanlist
*sl
= arg
;
2479 struct nlattr
**tb
= nl80211_parse(msg
);
2480 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2482 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2483 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2484 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2485 [NL80211_BSS_BSSID
] = { 0 },
2486 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2487 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2488 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2489 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2490 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2491 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2492 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2493 [NL80211_BSS_BEACON_IES
] = { 0 },
2496 if (!tb
[NL80211_ATTR_BSS
] ||
2497 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2499 !bss
[NL80211_BSS_BSSID
])
2504 if (bss
[NL80211_BSS_CAPABILITY
])
2505 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2509 memset(sl
->e
, 0, sizeof(*sl
->e
));
2510 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2513 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2514 else if (caps
& (1<<0))
2515 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2517 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2520 sl
->e
->crypto
.enabled
= 1;
2522 if (bss
[NL80211_BSS_FREQUENCY
])
2523 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2524 bss
[NL80211_BSS_FREQUENCY
]));
2526 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2527 nl80211_get_scanlist_ie(bss
, sl
->e
);
2529 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2532 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2534 rssi
= sl
->e
->signal
- 0x100;
2538 else if (rssi
> -40)
2541 sl
->e
->quality
= (rssi
+ 110);
2542 sl
->e
->quality_max
= 70;
2545 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2547 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2548 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2557 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2559 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2561 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2564 if (nl80211_wait("nl80211", "scan",
2565 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2568 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2569 nl80211_get_scanlist_cb
, &sl
))
2572 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2580 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2583 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2584 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2590 if (len
+ 1 >= outlen
)
2600 out
[len
++] = '\n'; in
++;
2604 out
[len
++] = '\r'; in
++;
2608 out
[len
++] = '\t'; in
++;
2612 out
[len
++] = '\033'; in
++;
2616 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2617 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2639 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2641 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2642 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2643 struct sockaddr_un local
= { 0 };
2644 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2646 sock
= nl80211_wpactl_connect(ifname
, &local
);
2651 send(sock
, "ATTACH", 6, 0);
2652 send(sock
, "SCAN", 4, 0);
2655 * wait for scan results:
2656 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2657 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2658 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2659 * time to process the results, so try 72 + 2 + 1 times.
2661 for (tries
= 0; tries
< 75; tries
++)
2663 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2666 /* got an event notification */
2667 if (reply
[0] == '<')
2669 /* scan results are ready */
2670 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2672 /* send "SCAN_RESULTS" command */
2673 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2677 /* is another unrelated event, retry */
2681 /* scanning already in progress, keep awaiting results */
2682 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2687 /* another failure, abort */
2688 else if (!strncmp(reply
, "FAIL-", 5))
2694 /* receive and parse scan results if the wait above didn't time out */
2695 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2697 /* received an event notification, receive again */
2698 if (reply
[0] == '<')
2701 nl80211_get_quality_max(ifname
, &qmax
);
2703 for (line
= strtok_r(reply
, "\n", &pos
);
2705 line
= strtok_r(NULL
, "\n", &pos
))
2707 /* skip header line */
2714 bssid
= strtok(line
, "\t");
2715 freq
= strtok(NULL
, "\t");
2716 signal
= strtok(NULL
, "\t");
2717 flags
= strtok(NULL
, "\t");
2718 ssid
= strtok(NULL
, "\n");
2720 if (!bssid
|| !freq
|| !signal
|| !flags
)
2724 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2725 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2726 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2727 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2728 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2729 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2733 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2738 if (strstr(flags
, "[MESH]"))
2739 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2740 else if (strstr(flags
, "[IBSS]"))
2741 e
->mode
= IWINFO_OPMODE_ADHOC
;
2743 e
->mode
= IWINFO_OPMODE_MASTER
;
2746 e
->channel
= nl80211_freq2channel(atoi(freq
));
2749 rssi
= atoi(signal
);
2755 /* The cfg80211 wext compat layer assumes a signal range
2756 * of -110 dBm to -40 dBm, the quality value is derived
2757 * by adding 110 to the signal level */
2760 else if (rssi
> -40)
2763 e
->quality
= (rssi
+ 110);
2771 e
->quality_max
= qmax
;
2774 nl80211_get_scancrypto(flags
, &e
->crypto
);
2780 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2785 unlink(local
.sun_path
);
2787 return (count
>= 0) ? 0 : -1;
2790 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2797 /* Got a radioX pseudo interface, find some interface on it or create one */
2798 if (!strncmp(ifname
, "radio", 5))
2800 /* Reuse existing interface */
2801 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2803 return nl80211_get_scanlist(res
, buf
, len
);
2806 /* Need to spawn a temporary iface for scanning */
2807 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2809 rv
= nl80211_get_scanlist(res
, buf
, len
);
2815 /* WPA supplicant */
2816 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2821 /* station / ad-hoc / monitor scan */
2822 else if (!nl80211_get_mode(ifname
, &mode
) &&
2823 (mode
== IWINFO_OPMODE_ADHOC
||
2824 mode
== IWINFO_OPMODE_MASTER
||
2825 mode
== IWINFO_OPMODE_CLIENT
||
2826 mode
== IWINFO_OPMODE_MONITOR
) &&
2827 iwinfo_ifup(ifname
))
2829 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2835 /* Got a temp interface, don't create yet another one */
2836 if (!strncmp(ifname
, "tmp.", 4))
2838 if (!iwinfo_ifup(ifname
))
2841 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2842 iwinfo_ifdown(ifname
);
2846 /* Spawn a new scan interface */
2849 if (!(res
= nl80211_ifadd(ifname
)))
2854 /* if we can take the new interface up, the driver supports an
2855 * additional interface and there's no need to tear down the ap */
2856 if (iwinfo_ifup(res
))
2858 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2862 /* driver cannot create secondary interface, take down ap
2864 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2866 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2868 iwinfo_ifup(ifname
);
2869 nl80211_hostapd_hup(ifname
);
2880 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2882 int bands_remain
, freqs_remain
;
2884 struct nl80211_array_buf
*arr
= arg
;
2885 struct iwinfo_freqlist_entry
*e
;
2887 struct nlattr
**attr
= nl80211_parse(msg
);
2888 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2889 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2890 struct nlattr
*band
, *freq
;
2895 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2896 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2898 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2899 nla_data(band
), nla_len(band
), NULL
);
2901 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2902 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2904 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2905 nla_data(freq
), nla_len(freq
), NULL
);
2907 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2908 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2911 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2912 e
->channel
= nl80211_freq2channel(e
->mhz
);
2915 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2916 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2919 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2920 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2921 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2922 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2923 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2924 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2925 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2926 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2927 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2928 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2929 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2930 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2942 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2944 struct nl80211_msg_conveyor
*cv
;
2945 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2946 uint32_t features
= nl80211_get_protocol_features(ifname
);
2949 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
2950 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
2954 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
2955 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
2958 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
2968 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
2971 struct nlattr
**attr
= nl80211_parse(msg
);
2973 if (attr
[NL80211_ATTR_REG_ALPHA2
])
2974 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
2981 static int nl80211_get_country(const char *ifname
, char *buf
)
2983 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
2984 nl80211_get_country_cb
, buf
))
2990 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
2993 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
2994 const struct iwinfo_iso3166_label
*l
;
2996 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
2998 e
->iso3166
= l
->iso3166
;
2999 e
->ccode
[0] = (l
->iso3166
/ 256);
3000 e
->ccode
[1] = (l
->iso3166
% 256);
3004 *len
= (count
* sizeof(struct iwinfo_country_entry
));
3009 struct nl80211_modes
3019 uint16_t he_phy_cap
[6];
3022 static int nl80211_eval_modelist(struct nl80211_modes
*m
)
3024 /* Treat any nonzero capability as 11n */
3027 m
->hw
|= IWINFO_80211_N
;
3028 m
->ht
|= IWINFO_HTMODE_HT20
;
3030 if (m
->nl_ht
& (1 << 1))
3031 m
->ht
|= IWINFO_HTMODE_HT40
;
3034 if (m
->he_phy_cap
[0] != 0) {
3035 m
->hw
|= IWINFO_80211_AX
;
3036 m
->ht
|= IWINFO_HTMODE_HE20
;
3038 if (m
->he_phy_cap
[0] & BIT(9))
3039 m
->ht
|= IWINFO_HTMODE_HE40
;
3040 if (m
->he_phy_cap
[0] & BIT(10))
3041 m
->ht
|= IWINFO_HTMODE_HE40
| IWINFO_HTMODE_HE80
;
3042 if (m
->he_phy_cap
[0] & BIT(11))
3043 m
->ht
|= IWINFO_HTMODE_HE160
;
3044 if (m
->he_phy_cap
[0] & BIT(12))
3045 m
->ht
|= IWINFO_HTMODE_HE160
| IWINFO_HTMODE_HE80_80
;
3048 if (m
->bands
& IWINFO_BAND_24
)
3050 m
->hw
|= IWINFO_80211_B
;
3051 m
->hw
|= IWINFO_80211_G
;
3056 /* Treat any nonzero capability as 11ac */
3059 m
->hw
|= IWINFO_80211_AC
;
3060 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3062 switch ((m
->nl_vht
>> 2) & 3)
3065 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3069 m
->ht
|= IWINFO_HTMODE_VHT160
;
3074 if (m
->bands
& IWINFO_BAND_60
)
3076 m
->hw
|= IWINFO_80211_AD
;
3079 if (!(m
->hw
& IWINFO_80211_AC
))
3081 m
->hw
|= IWINFO_80211_A
;
3085 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
3087 struct nl80211_modes
*m
= arg
;
3088 int bands_remain
, freqs_remain
;
3090 uint32_t vht_caps
= 0;
3091 struct nlattr
**attr
= nl80211_parse(msg
);
3092 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3093 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
3094 struct nlattr
*band
, *freq
;
3097 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
3099 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3101 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3102 nla_data(band
), nla_len(band
), NULL
);
3104 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
3105 m
->nl_ht
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
3107 if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
3108 m
->nl_vht
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
3110 if (bands
[NL80211_BAND_ATTR_IFTYPE_DATA
]) {
3111 struct nlattr
*tb
[NL80211_BAND_IFTYPE_ATTR_MAX
+ 1];
3112 struct nlattr
*nl_iftype
;
3116 nla_for_each_nested(nl_iftype
, bands
[NL80211_BAND_ATTR_IFTYPE_DATA
], rem_band
) {
3117 nla_parse(tb
, NL80211_BAND_IFTYPE_ATTR_MAX
,
3118 nla_data(nl_iftype
), nla_len(nl_iftype
), NULL
);
3119 if (tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]) {
3120 len
= nla_len(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]);
3122 if (len
> sizeof(m
->he_phy_cap
) - 1)
3123 len
= sizeof(m
->he_phy_cap
) - 1;
3124 memcpy(&((__u8
*)m
->he_phy_cap
)[1],
3125 nla_data(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]),
3131 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
3132 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
3135 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
3136 nla_data(freq
), nla_len(freq
), NULL
);
3138 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
3141 freq_mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
3143 if (freq_mhz
> 2400 && freq_mhz
< 2485)
3145 m
->bands
|= IWINFO_BAND_24
;
3147 else if (freq_mhz
> 5000 && freq_mhz
< 5850)
3149 m
->bands
|= IWINFO_BAND_5
;
3151 else if (freq_mhz
> 6000 && freq_mhz
< 7120)
3153 m
->bands
|= IWINFO_BAND_6
;
3155 else if (freq_mhz
>= 56160)
3157 m
->bands
|= IWINFO_BAND_60
;
3169 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3171 struct nl80211_msg_conveyor
*cv
;
3172 struct nl80211_modes m
= {};
3173 uint32_t features
= nl80211_get_protocol_features(ifname
);
3176 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3177 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3181 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3182 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3183 goto nla_put_failure
;
3185 nl80211_eval_modelist(&m
);
3202 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3204 struct nlattr
**tb
= nl80211_parse(msg
);
3206 struct chan_info
*chn
= arg
;
3208 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3209 chn
->width
= nla_get_u32(cur
);
3211 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3212 chn
->mode
= nla_get_u32(cur
);
3217 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3219 struct chan_info chn
= { .width
= 0, .mode
= 0 };
3223 res
= nl80211_phy2ifname(ifname
);
3226 err
= nl80211_request(res
? res
: ifname
,
3227 NL80211_CMD_GET_INTERFACE
, 0,
3228 nl80211_get_htmode_cb
, &chn
);
3232 switch (chn
.width
) {
3233 case NL80211_CHAN_WIDTH_20
:
3235 *buf
= IWINFO_HTMODE_VHT20
;
3237 *buf
= IWINFO_HTMODE_HT20
;
3239 case NL80211_CHAN_WIDTH_40
:
3241 *buf
= IWINFO_HTMODE_VHT40
;
3243 *buf
= IWINFO_HTMODE_HT40
;
3245 case NL80211_CHAN_WIDTH_80
:
3246 *buf
= IWINFO_HTMODE_VHT80
;
3248 case NL80211_CHAN_WIDTH_80P80
:
3249 *buf
= IWINFO_HTMODE_VHT80_80
;
3251 case NL80211_CHAN_WIDTH_160
:
3252 *buf
= IWINFO_HTMODE_VHT160
;
3254 case NL80211_CHAN_WIDTH_5
:
3255 case NL80211_CHAN_WIDTH_10
:
3256 case NL80211_CHAN_WIDTH_20_NOHT
:
3257 *buf
= IWINFO_HTMODE_NOHT
;
3266 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3268 struct nl80211_msg_conveyor
*cv
;
3269 struct nl80211_modes m
= {};
3270 uint32_t features
= nl80211_get_protocol_features(ifname
);
3273 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3274 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3278 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3279 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3280 goto nla_put_failure
;
3282 nl80211_eval_modelist(&m
);
3295 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3297 struct nlattr
**attr
= nl80211_parse(msg
);
3298 struct nlattr
*comb
;
3300 int comb_rem
, limit_rem
, mode_rem
;
3303 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3306 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3308 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3309 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3310 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3312 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3313 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3314 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3315 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3317 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3318 struct nlattr
*limit
;
3320 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3322 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3325 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3327 struct nlattr
*mode
;
3329 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3331 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3332 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3335 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3338 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3339 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3348 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3350 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3351 nl80211_get_ifcomb_cb
, buf
))
3357 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3359 char *phy
, compat
[64], path
[PATH_MAX
];
3362 /* Try to determine the phy name from the given interface */
3363 phy
= nl80211_ifname2phy(ifname
);
3365 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3366 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3368 if (nl80211_readstr(path
, compat
, sizeof(compat
)) <= 0)
3371 if (!strcmp(compat
, "qca,ar9130-wmac")) {
3372 id
->vendor_id
= 0x168c;
3373 id
->device_id
= 0x0029;
3374 id
->subsystem_vendor_id
= 0x168c;
3375 id
->subsystem_device_id
= 0x9130;
3376 } else if (!strcmp(compat
, "qca,ar9330-wmac")) {
3377 id
->vendor_id
= 0x168c;
3378 id
->device_id
= 0x0030;
3379 id
->subsystem_vendor_id
= 0x168c;
3380 id
->subsystem_device_id
= 0x9330;
3381 } else if (!strcmp(compat
, "qca,ar9340-wmac")) {
3382 id
->vendor_id
= 0x168c;
3383 id
->device_id
= 0x0030;
3384 id
->subsystem_vendor_id
= 0x168c;
3385 id
->subsystem_device_id
= 0x9340;
3386 } else if (!strcmp(compat
, "qca,qca9530-wmac")) {
3387 id
->vendor_id
= 0x168c;
3388 id
->device_id
= 0x0033;
3389 id
->subsystem_vendor_id
= 0x168c;
3390 id
->subsystem_device_id
= 0x9530;
3391 } else if (!strcmp(compat
, "qca,qca9550-wmac")) {
3392 id
->vendor_id
= 0x168c;
3393 id
->device_id
= 0x0033;
3394 id
->subsystem_vendor_id
= 0x168c;
3395 id
->subsystem_device_id
= 0x9550;
3396 } else if (!strcmp(compat
, "qca,qca9560-wmac")) {
3397 id
->vendor_id
= 0x168c;
3398 id
->device_id
= 0x0033;
3399 id
->subsystem_vendor_id
= 0x168c;
3400 id
->subsystem_device_id
= 0x9560;
3401 } else if (!strcmp(compat
, "qcom,ipq4019-wifi")) {
3402 id
->vendor_id
= 0x168c;
3403 id
->device_id
= 0x003c;
3404 id
->subsystem_vendor_id
= 0x168c;
3405 id
->subsystem_device_id
= 0x4019;
3406 } else if (!strcmp(compat
, "mediatek,mt7622-wmac")) {
3407 id
->vendor_id
= 0x14c3;
3408 id
->device_id
= 0x7622;
3409 id
->subsystem_vendor_id
= 0x14c3;
3410 id
->subsystem_device_id
= 0x7622;
3412 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
3416 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3418 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3419 char *phy
, num
[8], path
[PATH_MAX
];
3422 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3423 { "vendor", &id
->vendor_id
},
3424 { "device", &id
->device_id
},
3425 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3426 { "subsystem_device", &id
->subsystem_device_id
}
3429 memset(id
, 0, sizeof(*id
));
3431 /* Try to determine the phy name from the given interface */
3432 phy
= nl80211_ifname2phy(ifname
);
3434 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3436 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3437 phy
? "ieee80211" : "net",
3438 phy
? phy
: ifname
, lookup
[i
].path
);
3440 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3441 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3444 /* Failed to obtain hardware IDs, try FDT */
3445 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3446 if (!nl80211_hardware_id_from_fdt(id
, ifname
))
3449 /* Failed to obtain hardware IDs, search board config */
3450 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3451 return iwinfo_hardware_id_from_mtd(id
);
3456 static const struct iwinfo_hardware_entry
*
3457 nl80211_get_hardware_entry(const char *ifname
)
3459 struct iwinfo_hardware_id id
;
3461 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3464 return iwinfo_hardware(&id
);
3467 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3469 const struct iwinfo_hardware_entry
*hw
;
3471 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3472 sprintf(buf
, "Generic MAC80211");
3474 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3479 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3481 const struct iwinfo_hardware_entry
*hw
;
3483 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3486 *buf
= hw
->txpower_offset
;
3490 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3492 const struct iwinfo_hardware_entry
*hw
;
3494 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3497 *buf
= hw
->frequency_offset
;
3501 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3505 if (!strncmp(section
, "path=", 5))
3506 idx
= nl80211_phy_idx_from_path(section
+ 5);
3507 else if (!strncmp(section
, "macaddr=", 8))
3508 idx
= nl80211_phy_idx_from_macaddr(section
+ 8);
3510 idx
= nl80211_phy_idx_from_uci(section
);
3515 sprintf(buf
, "phy%d", idx
);
3519 static int nl80211_phy_path(const char *phyname
, const char **path
)
3521 if (strncmp(phyname
, "phy", 3) != 0)
3524 if (strchr(phyname
, '/'))
3527 *path
= nl80211_phy_path_str(phyname
);
3534 const struct iwinfo_ops nl80211_ops
= {
3536 .probe
= nl80211_probe
,
3537 .channel
= nl80211_get_channel
,
3538 .center_chan1
= nl80211_get_center_chan1
,
3539 .center_chan2
= nl80211_get_center_chan2
,
3540 .frequency
= nl80211_get_frequency
,
3541 .frequency_offset
= nl80211_get_frequency_offset
,
3542 .txpower
= nl80211_get_txpower
,
3543 .txpower_offset
= nl80211_get_txpower_offset
,
3544 .bitrate
= nl80211_get_bitrate
,
3545 .signal
= nl80211_get_signal
,
3546 .noise
= nl80211_get_noise
,
3547 .quality
= nl80211_get_quality
,
3548 .quality_max
= nl80211_get_quality_max
,
3549 .mbssid_support
= nl80211_get_mbssid_support
,
3550 .hwmodelist
= nl80211_get_hwmodelist
,
3551 .htmodelist
= nl80211_get_htmodelist
,
3552 .htmode
= nl80211_get_htmode
,
3553 .mode
= nl80211_get_mode
,
3554 .ssid
= nl80211_get_ssid
,
3555 .bssid
= nl80211_get_bssid
,
3556 .country
= nl80211_get_country
,
3557 .hardware_id
= nl80211_get_hardware_id
,
3558 .hardware_name
= nl80211_get_hardware_name
,
3559 .encryption
= nl80211_get_encryption
,
3560 .phyname
= nl80211_get_phyname
,
3561 .assoclist
= nl80211_get_assoclist
,
3562 .txpwrlist
= nl80211_get_txpwrlist
,
3563 .scanlist
= nl80211_get_scanlist
,
3564 .freqlist
= nl80211_get_freqlist
,
3565 .countrylist
= nl80211_get_countrylist
,
3566 .survey
= nl80211_get_survey
,
3567 .lookup_phy
= nl80211_lookup_phyname
,
3568 .phy_path
= nl80211_phy_path
,
3569 .close
= nl80211_close