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
[512], *link
;
248 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", phyname
);
249 phy_idx
= nl80211_readint(buf
);
253 buf_len
= snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/device", phyname
);
254 link
= realpath(buf
, path
);
258 if (strncmp(link
, prefix
, prefix_len
) != 0)
263 prefix
= "platform/";
264 prefix_len
= strlen(prefix
);
265 if (!strncmp(link
, prefix
, prefix_len
) && strstr(link
, "/pci"))
268 snprintf(buf
+ buf_len
, sizeof(buf
) - buf_len
, "/ieee80211");
273 while ((e
= readdir(d
)) != NULL
) {
276 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
277 cur_idx
= nl80211_readint(buf
);
281 if (cur_idx
>= phy_idx
)
292 offset
= link
- path
+ strlen(link
);
293 snprintf(path
+ offset
, sizeof(path
) - offset
, "+%d", seq
);
298 static int nl80211_phy_idx_from_path(const char *path
)
302 const char *cur_path
;
311 path_len
= strlen(path
);
315 d
= opendir("/sys/class/ieee80211");
319 while ((e
= readdir(d
)) != NULL
) {
320 cur_path
= nl80211_phy_path_str(e
->d_name
);
324 cur_path_len
= strlen(cur_path
);
325 if (cur_path_len
< path_len
)
328 if (strcmp(cur_path
+ cur_path_len
- path_len
, path
) != 0)
331 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
332 idx
= nl80211_readint(buf
);
343 static int nl80211_phy_idx_from_macaddr(const char *opt
)
352 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
353 if (glob(buf
, 0, NULL
, &gl
))
356 for (i
= 0; i
< gl
.gl_pathc
; i
++)
358 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
359 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
362 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
365 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
366 if ((idx
= nl80211_readint(buf
)) > -1)
375 static int nl80211_phy_idx_from_phy(const char *opt
)
382 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
383 return nl80211_readint(buf
);
386 static int nl80211_phy_idx_from_uci(const char *name
)
388 struct uci_section
*s
;
392 s
= iwinfo_uci_get_radio(name
, "mac80211");
396 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
397 idx
= nl80211_phy_idx_from_path(opt
);
401 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
402 idx
= nl80211_phy_idx_from_macaddr(opt
);
406 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
407 idx
= nl80211_phy_idx_from_phy(opt
);
414 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
417 unsigned int ifidx
= 0;
419 struct nl80211_msg_conveyor
*cv
;
424 if (nl80211_init() < 0)
427 if (!strncmp(ifname
, "mon.", 4))
428 ifidx
= if_nametoindex(&ifname
[4]);
430 ifidx
= if_nametoindex(ifname
);
433 phyidx
= nl80211_phy_idx_from_phy(ifname
);
435 phyidx
= nl80211_phy_idx_from_uci(ifname
);
438 /* Valid ifidx must be greater than 0 */
439 if ((ifidx
<= 0) && (phyidx
< 0))
442 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
447 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
448 else if (phyidx
> -1)
449 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
458 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
459 int (*cb_func
)(struct nl_msg
*, void *),
462 static struct nl80211_msg_conveyor rcv
;
466 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
468 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
470 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
477 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
478 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
479 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
482 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
489 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
490 int (*cb_func
)(struct nl_msg
*, void *),
493 struct nl80211_msg_conveyor
*cv
;
495 cv
= nl80211_msg(ifname
, cmd
, flags
);
500 return nl80211_send(cv
, cb_func
, cb_arg
);
503 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
505 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
506 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
508 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
509 genlmsg_attrlen(gnlh
, 0), NULL
);
514 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
516 uint32_t *features
= arg
;
517 struct nlattr
**attr
= nl80211_parse(msg
);
519 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
520 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
525 static int nl80211_get_protocol_features(const char *ifname
)
527 struct nl80211_msg_conveyor
*req
;
528 uint32_t features
= 0;
530 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
532 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
539 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
541 struct nl80211_group_conveyor
*cv
= arg
;
543 struct nlattr
**attr
= nl80211_parse(msg
);
544 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
548 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
551 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
553 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
554 nla_data(mgrp
), nla_len(mgrp
), NULL
);
556 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
557 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
558 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
559 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
561 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
569 static int nl80211_subscribe(const char *family
, const char *group
)
571 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
572 struct nl80211_msg_conveyor
*req
;
575 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
578 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
579 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
584 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
594 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
596 struct nl80211_event_conveyor
*cv
= arg
;
597 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
599 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
600 cv
->recv
= gnlh
->cmd
;
605 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
610 static int __nl80211_wait(const char *family
, const char *group
, ...)
612 struct nl80211_event_conveyor cv
= { };
618 if (nl80211_subscribe(family
, group
))
621 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
626 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
627 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
628 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
632 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
633 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
637 while (!cv
.recv
&& !err
)
638 nl_recvmsgs(nls
->nl_sock
, cb
);
645 #define nl80211_wait(family, group, ...) \
646 __nl80211_wait(family, group, __VA_ARGS__, 0)
649 static int nl80211_freq2channel(int freq
)
653 else if (freq
< 2484)
654 return (freq
- 2407) / 5;
655 else if (freq
>= 4910 && freq
<= 4980)
656 return (freq
- 4000) / 5;
657 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
658 return (freq
- 56160) / 2160;
660 return (freq
- 5000) / 5;
663 static int nl80211_channel2freq(int channel
, const char *band
)
665 if (!band
|| band
[0] != 'a')
669 else if (channel
< 14)
670 return (channel
* 5) + 2407;
672 else if ( strcmp(band
, "ad") == 0)
674 return 56160 + 2160 * channel
;
678 if (channel
>= 182 && channel
<= 196)
679 return (channel
* 5) + 4000;
681 return (channel
* 5) + 5000;
687 static int nl80211_phyname_cb(struct nl_msg
*msg
, void *arg
)
690 struct nlattr
**attr
= nl80211_parse(msg
);
692 if (attr
[NL80211_ATTR_WIPHY_NAME
])
693 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
694 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
701 static char * nl80211_ifname2phy(const char *ifname
)
703 static char phy
[32] = { 0 };
705 memset(phy
, 0, sizeof(phy
));
707 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
708 nl80211_phyname_cb
, phy
);
710 return phy
[0] ? phy
: NULL
;
713 static char * nl80211_phyidx2name(unsigned int idx
)
715 struct nl80211_msg_conveyor
*cv
;
716 static char phy
[32] = { 0 };
718 if (nl80211_init() < 0)
721 cv
= nl80211_new(nls
->nl80211
, NL80211_CMD_GET_WIPHY
, 0);
725 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, idx
);
727 memset(phy
, 0, sizeof(phy
));
728 nl80211_send(cv
, nl80211_phyname_cb
, phy
);
730 return phy
[0] ? phy
: NULL
;
736 static char * nl80211_phy2ifname(const char *ifname
)
738 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
740 static char nif
[IFNAMSIZ
] = { 0 };
745 /* Only accept phy name of the form phy%d or radio%d */
749 phyidx
= nl80211_phy_idx_from_phy(ifname
);
751 phyidx
= nl80211_phy_idx_from_uci(ifname
);;
755 memset(nif
, 0, sizeof(nif
));
759 if ((d
= opendir("/sys/class/net")) != NULL
)
761 while ((e
= readdir(d
)) != NULL
)
763 snprintf(buffer
, sizeof(buffer
),
764 "/sys/class/net/%s/phy80211/index", e
->d_name
);
766 if (nl80211_readint(buffer
) == phyidx
)
768 snprintf(buffer
, sizeof(buffer
),
769 "/sys/class/net/%s/ifindex", e
->d_name
);
771 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
772 ((ifidx
< 0) || (cifidx
< ifidx
)))
775 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
784 return nif
[0] ? nif
: NULL
;
787 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
790 struct nlattr
**tb
= nl80211_parse(msg
);
791 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
792 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
793 IWINFO_OPMODE_ADHOC
, /* IBSS */
794 IWINFO_OPMODE_CLIENT
, /* managed */
795 IWINFO_OPMODE_MASTER
, /* AP */
796 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
797 IWINFO_OPMODE_WDS
, /* WDS */
798 IWINFO_OPMODE_MONITOR
, /* monitor */
799 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
800 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
801 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
804 if (tb
[NL80211_ATTR_IFTYPE
])
805 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
811 static int nl80211_get_mode(const char *ifname
, int *buf
)
815 *buf
= IWINFO_OPMODE_UNKNOWN
;
817 res
= nl80211_phy2ifname(ifname
);
819 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
820 nl80211_get_mode_cb
, buf
);
822 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
825 static int __nl80211_hostapd_query(const char *ifname
, ...)
828 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
829 int len
, mode
, found
= 0, match
= 1;
832 if (nl80211_get_mode(ifname
, &mode
))
835 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
838 phy
= nl80211_ifname2phy(ifname
);
843 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
844 fp
= fopen(buf
, "r");
849 va_start(ap
, ifname
);
851 /* clear all destination buffers */
854 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
856 dest
= va_arg(ap_cur
, char *);
857 len
= va_arg(ap_cur
, int);
859 memset(dest
, 0, len
);
864 /* iterate applicable lines and copy found values into dest buffers */
865 while (fgets(buf
, sizeof(buf
), fp
))
867 key
= strtok(buf
, " =\t\n");
868 val
= strtok(NULL
, "\n");
870 if (!key
|| !val
|| !*key
|| *key
== '#')
873 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
874 match
= !strcmp(ifname
, val
);
881 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
883 dest
= va_arg(ap_cur
, char *);
884 len
= va_arg(ap_cur
, int);
886 if (!strcmp(search
, key
))
888 strncpy(dest
, val
, len
- 1);
904 #define nl80211_hostapd_query(ifname, ...) \
905 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
908 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
911 struct timeval tv
= { 0, 256000 };
916 memset(buf
, 0, blen
);
918 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
921 if (!FD_ISSET(sock
, &rfds
))
924 return recv(sock
, buf
, blen
- 1, 0);
927 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
929 struct sockaddr_un remote
= { 0 };
930 size_t remote_length
, local_length
;
932 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
936 remote
.sun_family
= AF_UNIX
;
937 remote_length
= sizeof(remote
.sun_family
) +
938 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
941 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
947 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
949 remote_length
= sizeof(remote
.sun_family
) +
950 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
952 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
959 local
->sun_family
= AF_UNIX
;
960 local_length
= sizeof(local
->sun_family
) +
961 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
963 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
972 static int __nl80211_wpactl_query(const char *ifname
, ...)
975 struct sockaddr_un local
= { 0 };
976 int len
, mode
, found
= 0, sock
= -1;
977 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
979 if (nl80211_get_mode(ifname
, &mode
))
982 if (mode
!= IWINFO_OPMODE_CLIENT
&&
983 mode
!= IWINFO_OPMODE_ADHOC
&&
984 mode
!= IWINFO_OPMODE_MESHPOINT
)
987 sock
= nl80211_wpactl_connect(ifname
, &local
);
992 va_start(ap
, ifname
);
994 /* clear all destination buffers */
997 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
999 dest
= va_arg(ap_cur
, char *);
1000 len
= va_arg(ap_cur
, int);
1002 memset(dest
, 0, len
);
1007 send(sock
, "STATUS", 6, 0);
1011 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
1017 for (line
= strtok_r(buf
, "\n", &pos
);
1019 line
= strtok_r(NULL
, "\n", &pos
))
1021 key
= strtok(line
, "=");
1022 val
= strtok(NULL
, "\n");
1027 va_copy(ap_cur
, ap
);
1029 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
1031 dest
= va_arg(ap_cur
, char *);
1032 len
= va_arg(ap_cur
, int);
1034 if (!strcmp(search
, key
))
1036 strncpy(dest
, val
, len
- 1);
1051 unlink(local
.sun_path
);
1056 #define nl80211_wpactl_query(ifname, ...) \
1057 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1060 static char * nl80211_ifadd(const char *ifname
)
1062 char path
[PATH_MAX
];
1063 static char nif
[IFNAMSIZ
] = { 0 };
1064 struct nl80211_msg_conveyor
*req
;
1067 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
1070 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
1072 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
1073 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
1075 nl80211_send(req
, NULL
, NULL
);
1077 snprintf(path
, sizeof(path
) - 1,
1078 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
1080 if ((sysfs
= fopen(path
, "w")) != NULL
)
1082 fwrite("0\n", 1, 2, sysfs
);
1095 static void nl80211_ifdel(const char *ifname
)
1097 struct nl80211_msg_conveyor
*req
;
1099 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1102 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1104 nl80211_send(req
, NULL
, NULL
);
1112 static void nl80211_hostapd_hup(const char *ifname
)
1116 char *phy
= nl80211_ifname2phy(ifname
);
1120 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1121 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1123 if (read(fd
, buf
, sizeof(buf
)) > 0)
1135 static int nl80211_probe(const char *ifname
)
1137 return !!nl80211_ifname2phy(ifname
);
1140 struct nl80211_ssid_bssid
{
1141 unsigned char *ssid
;
1142 unsigned char bssid
[7];
1145 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1147 struct nl80211_ssid_bssid
*sb
= arg
;
1148 struct nlattr
**tb
= nl80211_parse(msg
);
1150 if (tb
[NL80211_ATTR_MAC
]) {
1152 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1153 sizeof(sb
->bssid
) - 1);
1159 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1163 struct nl80211_ssid_bssid
*sb
= arg
;
1164 struct nlattr
**tb
= nl80211_parse(msg
);
1165 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1167 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1168 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1169 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1172 if (!tb
[NL80211_ATTR_BSS
] ||
1173 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1175 !bss
[NL80211_BSS_BSSID
] ||
1176 !bss
[NL80211_BSS_STATUS
] ||
1177 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1182 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1184 case NL80211_BSS_STATUS_ASSOCIATED
:
1185 case NL80211_BSS_STATUS_AUTHENTICATED
:
1186 case NL80211_BSS_STATUS_IBSS_JOINED
:
1190 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1191 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1193 while (ielen
>= 2 && ielen
>= ie
[1])
1197 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1208 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1217 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1220 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1222 /* try to find ssid from scan dump results */
1223 res
= nl80211_phy2ifname(ifname
);
1226 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1227 nl80211_get_ssid_bssid_cb
, &sb
);
1229 /* failed, try to find from hostapd info */
1230 if (sb
.ssid
[0] == 0)
1231 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1232 IWINFO_ESSID_MAX_SIZE
+ 1);
1234 /* failed, try to obtain Mesh ID */
1235 if (sb
.ssid
[0] == 0)
1236 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1237 buf
, IWINFO_ESSID_MAX_SIZE
+ 1);
1239 return (sb
.ssid
[0] == 0) ? -1 : 0;
1242 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1244 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1245 struct nl80211_ssid_bssid sb
= { };
1247 res
= nl80211_phy2ifname(ifname
);
1249 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1250 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1251 nl80211_get_macaddr_cb
, &sb
);
1253 /* failed, try to find bssid from scan dump results */
1254 if (sb
.bssid
[0] == 0)
1255 nl80211_request(res
? res
: ifname
,
1256 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1257 nl80211_get_ssid_bssid_cb
, &sb
);
1259 /* failed, try to find mac from hostapd info */
1260 if ((sb
.bssid
[0] == 0) &&
1261 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1264 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1265 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1266 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1267 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1268 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1269 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1274 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1275 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1276 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1285 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1288 struct nlattr
**attr
= nl80211_parse(msg
);
1289 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1291 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1292 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1293 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1296 if (attr
[NL80211_ATTR_BSS
] &&
1297 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1298 attr
[NL80211_ATTR_BSS
], bss_policy
))
1300 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1301 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1307 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1310 struct nlattr
**tb
= nl80211_parse(msg
);
1312 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1313 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1318 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1320 char *res
, channel
[4], hwmode
[3];
1322 /* try to find frequency from interface info */
1323 res
= nl80211_phy2ifname(ifname
);
1326 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1327 nl80211_get_frequency_info_cb
, buf
);
1329 /* failed, try to find frequency from hostapd info */
1331 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1332 "channel", channel
, sizeof(channel
)) == 2)
1334 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1337 /* failed, try to find frequency from scan results */
1340 res
= nl80211_phy2ifname(ifname
);
1342 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1343 nl80211_get_frequency_scan_cb
, buf
);
1346 return (*buf
== 0) ? -1 : 0;
1349 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1352 struct nlattr
**tb
= nl80211_parse(msg
);
1354 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1355 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1360 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1364 /* try to find frequency from interface info */
1365 res
= nl80211_phy2ifname(ifname
);
1368 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1369 nl80211_get_center_freq1_cb
, buf
);
1371 return (*buf
== 0) ? -1 : 0;
1374 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1377 struct nlattr
**tb
= nl80211_parse(msg
);
1379 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1380 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1385 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1389 /* try to find frequency from interface info */
1390 res
= nl80211_phy2ifname(ifname
);
1393 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1394 nl80211_get_center_freq2_cb
, buf
);
1396 return (*buf
== 0) ? -1 : 0;
1399 static int nl80211_get_channel(const char *ifname
, int *buf
)
1401 if (!nl80211_get_frequency(ifname
, buf
))
1403 *buf
= nl80211_freq2channel(*buf
);
1410 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1412 if (!nl80211_get_center_freq1(ifname
, buf
))
1414 *buf
= nl80211_freq2channel(*buf
);
1421 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1423 if (!nl80211_get_center_freq2(ifname
, buf
))
1425 *buf
= nl80211_freq2channel(*buf
);
1432 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1435 struct nlattr
**tb
= nl80211_parse(msg
);
1437 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1438 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1443 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1447 res
= nl80211_phy2ifname(ifname
);
1450 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1451 nl80211_get_txpower_cb
, buf
))
1458 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1462 struct nl80211_rssi_rate
*rr
= arg
;
1463 struct nlattr
**attr
= nl80211_parse(msg
);
1464 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1465 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1467 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1468 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1469 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1470 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1471 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1472 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1473 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1474 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1475 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1476 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1477 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1480 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1481 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1482 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1483 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1484 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1487 if (attr
[NL80211_ATTR_STA_INFO
])
1489 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1490 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1492 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1494 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1495 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1499 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1501 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1502 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1505 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1507 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1508 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1519 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1524 memset(r
, 0, sizeof(*r
));
1526 if ((d
= opendir("/sys/class/net")) != NULL
)
1528 while ((de
= readdir(d
)) != NULL
)
1530 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1531 (!de
->d_name
[strlen(ifname
)] ||
1532 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1534 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1535 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1543 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1545 struct nl80211_rssi_rate rr
;
1547 nl80211_fill_signal(ifname
, &rr
);
1549 if (rr
.rate_samples
)
1551 *buf
= (rr
.rate
* 100);
1558 static int nl80211_get_signal(const char *ifname
, int *buf
)
1560 struct nl80211_rssi_rate rr
;
1562 nl80211_fill_signal(ifname
, &rr
);
1564 if (rr
.rssi_samples
)
1573 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1575 int8_t *noise
= arg
;
1576 struct nlattr
**tb
= nl80211_parse(msg
);
1577 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1579 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1580 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1581 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1584 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1587 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1588 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1591 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1594 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1595 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1601 static int nl80211_get_noise(const char *ifname
, int *buf
)
1605 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1606 nl80211_get_noise_cb
, &noise
))
1617 static int nl80211_get_quality(const char *ifname
, int *buf
)
1621 if (!nl80211_get_signal(ifname
, &signal
))
1623 /* A positive signal level is usually just a quality
1624 * value, pass through as-is */
1630 /* The cfg80211 wext compat layer assumes a signal range
1631 * of -110 dBm to -40 dBm, the quality value is derived
1632 * by adding 110 to the signal level */
1637 else if (signal
> -40)
1640 *buf
= (signal
+ 110);
1649 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1651 /* The cfg80211 wext compat layer assumes a maximum
1658 static int nl80211_check_wepkey(const char *key
)
1662 switch (strlen(key
))
1666 return IWINFO_CIPHER_WEP40
;
1670 return IWINFO_CIPHER_WEP104
;
1681 } wpa_key_mgmt_strings
[] = {
1682 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1683 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1684 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1685 { "EAP-SHA384", 4, IWINFO_KMGMT_8021x
},
1686 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1687 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1688 { "NONE", 0, IWINFO_KMGMT_NONE
},
1689 { "None", 0, IWINFO_KMGMT_NONE
},
1690 { "PSK", 0, IWINFO_KMGMT_PSK
},
1691 { "EAP", 0, IWINFO_KMGMT_8021x
},
1692 { "SAE", 4, IWINFO_KMGMT_SAE
},
1693 { "OWE", 4, IWINFO_KMGMT_OWE
}
1696 static void parse_wpa_suites(const char *str
, int defversion
,
1697 uint8_t *versions
, uint8_t *suites
)
1701 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1707 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1709 m
= wpa_key_mgmt_strings
[i
].match
;
1712 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1714 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1715 version
= wpa_key_mgmt_strings
[i
].version
;
1717 version
= defversion
;
1719 *versions
|= version
;
1720 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1728 q
+= strcspn(q
, sep
);
1730 p
= q
+ strspn(q
, sep
);
1737 } wpa_cipher_strings
[] = {
1738 { "WEP-104", IWINFO_CIPHER_WEP104
},
1739 { "WEP-40", IWINFO_CIPHER_WEP40
},
1740 { "NONE", IWINFO_CIPHER_NONE
},
1741 { "TKIP", IWINFO_CIPHER_TKIP
},
1742 { "CCMP-256",IWINFO_CIPHER_CCMP256
},
1743 { "CCMP", IWINFO_CIPHER_CCMP
},
1744 { "GCMP-256",IWINFO_CIPHER_GCMP256
},
1745 { "GCMP", IWINFO_CIPHER_GCMP
}
1748 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1752 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1758 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1760 m
= wpa_cipher_strings
[i
].match
;
1763 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1765 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1773 q
+= strcspn(q
, sep
);
1775 p
= q
+ strspn(q
, sep
);
1779 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1783 uint8_t wpa_version
= 0;
1784 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1785 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1788 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1790 /* WPA supplicant */
1791 if (nl80211_wpactl_query(ifname
,
1792 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1793 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1794 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1795 "mode", mode
, sizeof(mode
)))
1798 if (!strcmp(wpa_key_mgmt
, "NONE"))
1800 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1801 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1803 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1805 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1806 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1809 c
->pair_ciphers
= 0;
1810 c
->group_ciphers
= 0;
1815 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1819 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1820 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1821 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1827 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1828 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1832 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1837 else if (!strncmp(p
, "WPA-", 4))
1843 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1845 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1852 else if (nl80211_hostapd_query(ifname
,
1853 "wpa", wpa
, sizeof(wpa
),
1854 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1855 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1856 "auth_algs", auth_algs
, sizeof(auth_algs
),
1857 "wep_key0", wep_key0
, sizeof(wep_key0
),
1858 "wep_key1", wep_key1
, sizeof(wep_key1
),
1859 "wep_key2", wep_key2
, sizeof(wep_key2
),
1860 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1864 if (wpa_key_mgmt
[0])
1866 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1868 if (!strncmp(p
, "WPA-", 4))
1871 if (!strncmp(p
, "FT-", 3))
1874 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1877 c
->enabled
= c
->wpa_version
? 1 : 0;
1880 if (wpa_pairwise
[0])
1881 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1885 switch (atoi(auth_algs
))
1888 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1892 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1896 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1897 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1901 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1902 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1903 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1904 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1906 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1909 c
->group_ciphers
= c
->pair_ciphers
;
1914 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1915 else if (!nl80211_get_mode(ifname
, &opmode
) &&
1916 (opmode
== IWINFO_OPMODE_ADHOC
||
1917 opmode
== IWINFO_OPMODE_MESHPOINT
))
1928 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1932 name
= nl80211_ifname2phy(ifname
);
1939 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1941 name
= nl80211_ifname2phy(name
);
1945 strcpy(buf
, ifname
);
1954 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1955 struct iwinfo_rate_entry
*re
)
1957 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1958 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1959 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1960 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1962 if (ri
[NL80211_RATE_INFO_HE_MCS
])
1965 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_MCS
]);
1967 if (ri
[NL80211_RATE_INFO_HE_NSS
])
1968 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_NSS
]);
1969 if (ri
[NL80211_RATE_INFO_HE_GI
])
1970 re
->he_gi
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_GI
]);
1971 if (ri
[NL80211_RATE_INFO_HE_DCM
])
1972 re
->he_dcm
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_DCM
]);
1974 else if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1977 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1979 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1980 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1982 else if (ri
[NL80211_RATE_INFO_MCS
])
1985 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1988 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1990 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1992 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1994 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1996 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1997 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
2002 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
2003 re
->is_short_gi
= 1;
2005 re
->is_40mhz
= (re
->mhz
== 40);
2008 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
2010 struct nl80211_array_buf
*arr
= arg
;
2011 struct iwinfo_survey_entry
*e
= arr
->buf
;
2012 struct nlattr
**attr
= nl80211_parse(msg
);
2013 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
2016 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
2017 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
2018 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
2019 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
2020 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
2021 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
2022 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
2023 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
2026 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
2027 attr
[NL80211_ATTR_SURVEY_INFO
],
2032 /* advance to end of array */
2034 memset(e
, 0, sizeof(*e
));
2036 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
2037 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
2039 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
2040 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
2042 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
2043 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
2045 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
2046 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
2048 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
2049 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
2051 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
2052 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
2054 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
2055 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
2062 static void plink_state_to_str(char *dst
, unsigned state
)
2065 case NL80211_PLINK_LISTEN
:
2066 strcpy(dst
, "LISTEN");
2068 case NL80211_PLINK_OPN_SNT
:
2069 strcpy(dst
, "OPN_SNT");
2071 case NL80211_PLINK_OPN_RCVD
:
2072 strcpy(dst
, "OPN_RCVD");
2074 case NL80211_PLINK_CNF_RCVD
:
2075 strcpy(dst
, "CNF_RCVD");
2077 case NL80211_PLINK_ESTAB
:
2078 strcpy(dst
, "ESTAB");
2080 case NL80211_PLINK_HOLDING
:
2081 strcpy(dst
, "HOLDING");
2083 case NL80211_PLINK_BLOCKED
:
2084 strcpy(dst
, "BLOCKED");
2087 strcpy(dst
, "UNKNOWN");
2092 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
2094 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
2097 case NL80211_MESH_POWER_ACTIVE
:
2098 strcpy(dst
, "ACTIVE");
2100 case NL80211_MESH_POWER_LIGHT_SLEEP
:
2101 strcpy(dst
, "LIGHT SLEEP");
2103 case NL80211_MESH_POWER_DEEP_SLEEP
:
2104 strcpy(dst
, "DEEP SLEEP");
2107 strcpy(dst
, "UNKNOWN");
2112 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2114 struct nl80211_array_buf
*arr
= arg
;
2115 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2116 struct nlattr
**attr
= nl80211_parse(msg
);
2117 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2118 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2119 struct nl80211_sta_flag_update
*sta_flags
;
2121 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2122 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2123 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2124 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2125 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2126 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2127 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2128 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2129 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2130 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2131 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2132 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2133 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2134 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2135 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2136 [NL80211_STA_INFO_STA_FLAGS
] =
2137 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2138 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2140 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2141 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2142 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2143 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2144 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2145 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2148 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2149 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2150 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2151 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2152 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2155 /* advance to end of array */
2157 memset(e
, 0, sizeof(*e
));
2159 if (attr
[NL80211_ATTR_MAC
])
2160 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2162 if (attr
[NL80211_ATTR_STA_INFO
] &&
2163 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2164 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2166 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2167 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2169 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2170 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2172 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2173 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2175 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2176 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2178 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2179 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2181 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2182 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2184 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2185 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2186 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2187 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2189 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2190 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2191 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2192 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2194 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2195 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2197 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2198 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2200 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2201 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2203 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2204 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2206 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2207 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2209 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2210 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2212 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2213 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2216 if (sinfo
[NL80211_STA_INFO_LLID
])
2217 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2219 if (sinfo
[NL80211_STA_INFO_PLID
])
2220 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2222 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2223 plink_state_to_str(e
->plink_state
,
2224 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2226 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2227 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2228 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2229 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2230 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2231 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2234 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2236 sta_flags
= (struct nl80211_sta_flag_update
*)
2237 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2239 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2240 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2241 e
->is_authorized
= 1;
2243 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2244 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2245 e
->is_authenticated
= 1;
2247 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2248 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2249 e
->is_preamble_short
= 1;
2251 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2252 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2255 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2256 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2259 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2260 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2265 e
->noise
= 0; /* filled in by caller */
2271 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2273 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2276 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2277 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2279 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2286 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2291 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2292 struct iwinfo_assoclist_entry
*e
;
2294 if ((d
= opendir("/sys/class/net")) != NULL
)
2296 while ((de
= readdir(d
)) != NULL
)
2298 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2299 (!de
->d_name
[strlen(ifname
)] ||
2300 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2302 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2303 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2309 if (!nl80211_get_noise(ifname
, &noise
))
2310 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2313 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2320 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2323 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2325 struct nlattr
**attr
= nl80211_parse(msg
);
2326 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2327 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2328 struct nlattr
*band
, *freq
;
2330 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2331 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2332 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2333 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2334 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2335 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2336 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2339 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2342 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2344 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2345 nla_len(band
), NULL
);
2347 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2349 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2350 nla_data(freq
), nla_len(freq
), freq_policy
);
2352 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2353 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2355 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2356 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2358 *dbm_max
= (int)(0.01 * nla_get_u32(
2359 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2369 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2372 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2373 struct iwinfo_txpwrlist_entry entry
;
2375 if (nl80211_get_channel(ifname
, &ch_cur
))
2378 /* initialize the value pointer with channel for callback */
2381 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2382 nl80211_get_txpwrlist_cb
, &dbm_max
);
2386 for (dbm_cur
= 0, dbm_cnt
= 0;
2388 dbm_cur
++, dbm_cnt
++)
2390 entry
.dbm
= dbm_cur
;
2391 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2393 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2396 entry
.dbm
= dbm_max
;
2397 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2399 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2402 *len
= dbm_cnt
* sizeof(entry
);
2409 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2411 int wpa_version
= 0;
2412 char *p
, *q
, *proto
, *suites
;
2416 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2417 if (!strcmp(p
, "WEP")) {
2419 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2420 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2421 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2425 proto
= strtok(p
, "-");
2426 suites
= strtok(NULL
, "]");
2428 if (!proto
|| !suites
)
2431 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2433 else if (!strcmp(proto
, "WPA"))
2440 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2441 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2446 struct nl80211_scanlist
{
2447 struct iwinfo_scanlist_entry
*e
;
2452 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2453 struct iwinfo_scanlist_entry
*e
)
2455 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2456 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2457 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2460 while (ielen
>= 2 && ielen
>= ie
[1])
2465 case 114: /* Mesh ID */
2466 if (e
->ssid
[0] == 0) {
2467 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2468 memcpy(e
->ssid
, ie
+ 2, len
);
2474 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2475 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2478 case 221: /* Vendor */
2479 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2480 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2481 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2483 case 61: /* HT oeration */
2485 e
->ht_chan_info
.primary_chan
= ie
[2];
2486 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2487 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2490 case 192: /* VHT operation */
2492 e
->vht_chan_info
.chan_width
= ie
[2];
2493 e
->vht_chan_info
.center_chan_1
= ie
[3];
2494 e
->vht_chan_info
.center_chan_2
= ie
[4];
2504 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2509 struct nl80211_scanlist
*sl
= arg
;
2510 struct nlattr
**tb
= nl80211_parse(msg
);
2511 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2513 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2514 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2515 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2516 [NL80211_BSS_BSSID
] = { 0 },
2517 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2518 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2519 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2520 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2521 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2522 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2523 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2524 [NL80211_BSS_BEACON_IES
] = { 0 },
2527 if (!tb
[NL80211_ATTR_BSS
] ||
2528 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2530 !bss
[NL80211_BSS_BSSID
])
2535 if (bss
[NL80211_BSS_CAPABILITY
])
2536 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2540 memset(sl
->e
, 0, sizeof(*sl
->e
));
2541 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2544 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2545 else if (caps
& (1<<0))
2546 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2548 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2551 sl
->e
->crypto
.enabled
= 1;
2553 if (bss
[NL80211_BSS_FREQUENCY
])
2554 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2555 bss
[NL80211_BSS_FREQUENCY
]));
2557 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2558 nl80211_get_scanlist_ie(bss
, sl
->e
);
2560 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2563 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2565 rssi
= sl
->e
->signal
- 0x100;
2569 else if (rssi
> -40)
2572 sl
->e
->quality
= (rssi
+ 110);
2573 sl
->e
->quality_max
= 70;
2576 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2578 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2579 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2588 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2590 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2592 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2595 if (nl80211_wait("nl80211", "scan",
2596 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2599 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2600 nl80211_get_scanlist_cb
, &sl
))
2603 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2611 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2614 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2615 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2621 if (len
+ 1 >= outlen
)
2631 out
[len
++] = '\n'; in
++;
2635 out
[len
++] = '\r'; in
++;
2639 out
[len
++] = '\t'; in
++;
2643 out
[len
++] = '\033'; in
++;
2647 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2648 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2670 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2672 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2673 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2674 struct sockaddr_un local
= { 0 };
2675 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2677 sock
= nl80211_wpactl_connect(ifname
, &local
);
2682 send(sock
, "ATTACH", 6, 0);
2683 send(sock
, "SCAN", 4, 0);
2686 * wait for scan results:
2687 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2688 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2689 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2690 * time to process the results, so try 72 + 2 + 1 times.
2692 for (tries
= 0; tries
< 75; tries
++)
2694 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2697 /* got an event notification */
2698 if (reply
[0] == '<')
2700 /* scan results are ready */
2701 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2703 /* send "SCAN_RESULTS" command */
2704 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2708 /* is another unrelated event, retry */
2712 /* scanning already in progress, keep awaiting results */
2713 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2718 /* another failure, abort */
2719 else if (!strncmp(reply
, "FAIL-", 5))
2725 /* receive and parse scan results if the wait above didn't time out */
2726 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2728 /* received an event notification, receive again */
2729 if (reply
[0] == '<')
2732 nl80211_get_quality_max(ifname
, &qmax
);
2734 for (line
= strtok_r(reply
, "\n", &pos
);
2736 line
= strtok_r(NULL
, "\n", &pos
))
2738 /* skip header line */
2745 bssid
= strtok(line
, "\t");
2746 freq
= strtok(NULL
, "\t");
2747 signal
= strtok(NULL
, "\t");
2748 flags
= strtok(NULL
, "\t");
2749 ssid
= strtok(NULL
, "\n");
2751 if (!bssid
|| !freq
|| !signal
|| !flags
)
2755 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2756 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2757 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2758 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2759 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2760 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2764 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2769 if (strstr(flags
, "[MESH]"))
2770 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2771 else if (strstr(flags
, "[IBSS]"))
2772 e
->mode
= IWINFO_OPMODE_ADHOC
;
2774 e
->mode
= IWINFO_OPMODE_MASTER
;
2777 e
->channel
= nl80211_freq2channel(atoi(freq
));
2780 rssi
= atoi(signal
);
2786 /* The cfg80211 wext compat layer assumes a signal range
2787 * of -110 dBm to -40 dBm, the quality value is derived
2788 * by adding 110 to the signal level */
2791 else if (rssi
> -40)
2794 e
->quality
= (rssi
+ 110);
2802 e
->quality_max
= qmax
;
2805 nl80211_get_scancrypto(flags
, &e
->crypto
);
2811 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2816 unlink(local
.sun_path
);
2818 return (count
>= 0) ? 0 : -1;
2821 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2828 /* Got a radioX pseudo interface, find some interface on it or create one */
2829 if (!strncmp(ifname
, "radio", 5))
2831 /* Reuse existing interface */
2832 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2834 return nl80211_get_scanlist(res
, buf
, len
);
2837 /* Need to spawn a temporary iface for scanning */
2838 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2840 rv
= nl80211_get_scanlist(res
, buf
, len
);
2846 /* WPA supplicant */
2847 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2852 /* station / ad-hoc / monitor scan */
2853 else if (!nl80211_get_mode(ifname
, &mode
) &&
2854 (mode
== IWINFO_OPMODE_ADHOC
||
2855 mode
== IWINFO_OPMODE_MASTER
||
2856 mode
== IWINFO_OPMODE_CLIENT
||
2857 mode
== IWINFO_OPMODE_MONITOR
) &&
2858 iwinfo_ifup(ifname
))
2860 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2866 /* Got a temp interface, don't create yet another one */
2867 if (!strncmp(ifname
, "tmp.", 4))
2869 if (!iwinfo_ifup(ifname
))
2872 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2873 iwinfo_ifdown(ifname
);
2877 /* Spawn a new scan interface */
2880 if (!(res
= nl80211_ifadd(ifname
)))
2885 /* if we can take the new interface up, the driver supports an
2886 * additional interface and there's no need to tear down the ap */
2887 if (iwinfo_ifup(res
))
2889 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2893 /* driver cannot create secondary interface, take down ap
2895 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2897 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2899 iwinfo_ifup(ifname
);
2900 nl80211_hostapd_hup(ifname
);
2913 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2915 int bands_remain
, freqs_remain
;
2917 struct nl80211_array_buf
*arr
= arg
;
2918 struct iwinfo_freqlist_entry
*e
;
2920 struct nlattr
**attr
= nl80211_parse(msg
);
2921 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2922 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2923 struct nlattr
*band
, *freq
;
2928 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2929 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2931 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2932 nla_data(band
), nla_len(band
), NULL
);
2934 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2935 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2937 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2938 nla_data(freq
), nla_len(freq
), NULL
);
2940 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2941 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2944 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2945 e
->channel
= nl80211_freq2channel(e
->mhz
);
2948 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2949 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2952 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2953 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2954 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2955 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2956 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2957 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2958 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2959 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2960 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2961 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2962 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2963 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2975 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2977 struct nl80211_msg_conveyor
*cv
;
2978 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2979 uint32_t features
= nl80211_get_protocol_features(ifname
);
2982 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
2983 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
2987 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
2988 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
2991 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
3001 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
3004 struct nlattr
**attr
= nl80211_parse(msg
);
3006 if (attr
[NL80211_ATTR_REG_ALPHA2
])
3007 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
3014 static int nl80211_get_country(const char *ifname
, char *buf
)
3016 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
3017 nl80211_get_country_cb
, buf
))
3023 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
3026 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
3027 const struct iwinfo_iso3166_label
*l
;
3029 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
3031 e
->iso3166
= l
->iso3166
;
3032 e
->ccode
[0] = (l
->iso3166
/ 256);
3033 e
->ccode
[1] = (l
->iso3166
% 256);
3037 *len
= (count
* sizeof(struct iwinfo_country_entry
));
3042 struct nl80211_modes
3052 uint16_t he_phy_cap
[6];
3055 static int nl80211_eval_modelist(struct nl80211_modes
*m
)
3057 /* Treat any nonzero capability as 11n */
3060 m
->hw
|= IWINFO_80211_N
;
3061 m
->ht
|= IWINFO_HTMODE_HT20
;
3063 if (m
->nl_ht
& (1 << 1))
3064 m
->ht
|= IWINFO_HTMODE_HT40
;
3067 if (m
->he_phy_cap
[0] != 0) {
3068 m
->hw
|= IWINFO_80211_AX
;
3069 m
->ht
|= IWINFO_HTMODE_HE20
;
3071 if (m
->he_phy_cap
[0] & BIT(9))
3072 m
->ht
|= IWINFO_HTMODE_HE40
;
3073 if (m
->he_phy_cap
[0] & BIT(10))
3074 m
->ht
|= IWINFO_HTMODE_HE40
| IWINFO_HTMODE_HE80
;
3075 if (m
->he_phy_cap
[0] & BIT(11))
3076 m
->ht
|= IWINFO_HTMODE_HE160
;
3077 if (m
->he_phy_cap
[0] & BIT(12))
3078 m
->ht
|= IWINFO_HTMODE_HE160
| IWINFO_HTMODE_HE80_80
;
3081 if (m
->bands
& IWINFO_BAND_24
)
3083 m
->hw
|= IWINFO_80211_B
;
3084 m
->hw
|= IWINFO_80211_G
;
3087 if (m
->bands
& IWINFO_BAND_5
)
3089 /* Treat any nonzero capability as 11ac */
3092 m
->hw
|= IWINFO_80211_AC
;
3093 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3095 switch ((m
->nl_vht
>> 2) & 3)
3098 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3102 m
->ht
|= IWINFO_HTMODE_VHT160
;
3107 m
->hw
|= IWINFO_80211_A
;
3111 if (m
->bands
& IWINFO_BAND_60
)
3113 m
->hw
|= IWINFO_80211_AD
;
3118 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
3120 struct nl80211_modes
*m
= arg
;
3121 int bands_remain
, freqs_remain
;
3122 struct nlattr
**attr
= nl80211_parse(msg
);
3123 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3124 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
3125 struct nlattr
*band
, *freq
;
3128 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
3130 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3132 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3133 nla_data(band
), nla_len(band
), NULL
);
3135 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
3136 m
->nl_ht
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
3138 if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
3139 m
->nl_vht
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
3141 if (bands
[NL80211_BAND_ATTR_IFTYPE_DATA
]) {
3142 struct nlattr
*tb
[NL80211_BAND_IFTYPE_ATTR_MAX
+ 1];
3143 struct nlattr
*nl_iftype
;
3147 nla_for_each_nested(nl_iftype
, bands
[NL80211_BAND_ATTR_IFTYPE_DATA
], rem_band
) {
3148 nla_parse(tb
, NL80211_BAND_IFTYPE_ATTR_MAX
,
3149 nla_data(nl_iftype
), nla_len(nl_iftype
), NULL
);
3150 if (tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]) {
3151 len
= nla_len(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]);
3153 if (len
> sizeof(m
->he_phy_cap
) - 1)
3154 len
= sizeof(m
->he_phy_cap
) - 1;
3155 memcpy(&((__u8
*)m
->he_phy_cap
)[1],
3156 nla_data(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]),
3162 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
3163 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
3166 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
3167 nla_data(freq
), nla_len(freq
), NULL
);
3169 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
3172 freq_mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
3174 if (freq_mhz
> 2400 && freq_mhz
< 2485)
3176 m
->bands
|= IWINFO_BAND_24
;
3178 else if (freq_mhz
> 5000 && freq_mhz
< 5850)
3180 m
->bands
|= IWINFO_BAND_5
;
3182 else if (freq_mhz
> 6000 && freq_mhz
< 7120)
3184 m
->bands
|= IWINFO_BAND_6
;
3186 else if (freq_mhz
>= 56160)
3188 m
->bands
|= IWINFO_BAND_60
;
3200 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3202 struct nl80211_msg_conveyor
*cv
;
3203 struct nl80211_modes m
= {};
3204 uint32_t features
= nl80211_get_protocol_features(ifname
);
3207 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3208 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3212 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3213 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3214 goto nla_put_failure
;
3216 nl80211_eval_modelist(&m
);
3233 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3235 struct nlattr
**tb
= nl80211_parse(msg
);
3237 struct chan_info
*chn
= arg
;
3239 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3240 chn
->width
= nla_get_u32(cur
);
3242 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3243 chn
->mode
= nla_get_u32(cur
);
3248 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3250 struct chan_info chn
= { .width
= 0, .mode
= 0 };
3254 res
= nl80211_phy2ifname(ifname
);
3257 err
= nl80211_request(res
? res
: ifname
,
3258 NL80211_CMD_GET_INTERFACE
, 0,
3259 nl80211_get_htmode_cb
, &chn
);
3263 switch (chn
.width
) {
3264 case NL80211_CHAN_WIDTH_20
:
3266 *buf
= IWINFO_HTMODE_VHT20
;
3268 *buf
= IWINFO_HTMODE_HT20
;
3270 case NL80211_CHAN_WIDTH_40
:
3272 *buf
= IWINFO_HTMODE_VHT40
;
3274 *buf
= IWINFO_HTMODE_HT40
;
3276 case NL80211_CHAN_WIDTH_80
:
3277 *buf
= IWINFO_HTMODE_VHT80
;
3279 case NL80211_CHAN_WIDTH_80P80
:
3280 *buf
= IWINFO_HTMODE_VHT80_80
;
3282 case NL80211_CHAN_WIDTH_160
:
3283 *buf
= IWINFO_HTMODE_VHT160
;
3285 case NL80211_CHAN_WIDTH_5
:
3286 case NL80211_CHAN_WIDTH_10
:
3287 case NL80211_CHAN_WIDTH_20_NOHT
:
3288 *buf
= IWINFO_HTMODE_NOHT
;
3297 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3299 struct nl80211_msg_conveyor
*cv
;
3300 struct nl80211_modes m
= {};
3301 uint32_t features
= nl80211_get_protocol_features(ifname
);
3304 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3305 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3309 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3310 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3311 goto nla_put_failure
;
3313 nl80211_eval_modelist(&m
);
3326 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3328 struct nlattr
**attr
= nl80211_parse(msg
);
3329 struct nlattr
*comb
;
3331 int comb_rem
, limit_rem
, mode_rem
;
3334 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3337 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3339 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3340 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3341 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3343 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3344 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3345 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3346 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3348 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3349 struct nlattr
*limit
;
3351 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3353 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3356 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3358 struct nlattr
*mode
;
3360 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3362 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3363 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3366 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3369 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3370 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3379 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3381 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3382 nl80211_get_ifcomb_cb
, buf
))
3388 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3390 char *phy
, compat
[64], path
[PATH_MAX
];
3392 /* Try to determine the phy name from the given interface */
3393 phy
= nl80211_ifname2phy(ifname
);
3395 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3396 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3398 if (nl80211_readstr(path
, compat
, sizeof(compat
)) <= 0)
3401 if (!strcmp(compat
, "qca,ar9130-wmac")) {
3402 id
->vendor_id
= 0x168c;
3403 id
->device_id
= 0x0029;
3404 id
->subsystem_vendor_id
= 0x168c;
3405 id
->subsystem_device_id
= 0x9130;
3406 } else if (!strcmp(compat
, "qca,ar9330-wmac")) {
3407 id
->vendor_id
= 0x168c;
3408 id
->device_id
= 0x0030;
3409 id
->subsystem_vendor_id
= 0x168c;
3410 id
->subsystem_device_id
= 0x9330;
3411 } else if (!strcmp(compat
, "qca,ar9340-wmac")) {
3412 id
->vendor_id
= 0x168c;
3413 id
->device_id
= 0x0030;
3414 id
->subsystem_vendor_id
= 0x168c;
3415 id
->subsystem_device_id
= 0x9340;
3416 } else if (!strcmp(compat
, "qca,qca9530-wmac")) {
3417 id
->vendor_id
= 0x168c;
3418 id
->device_id
= 0x0033;
3419 id
->subsystem_vendor_id
= 0x168c;
3420 id
->subsystem_device_id
= 0x9530;
3421 } else if (!strcmp(compat
, "qca,qca9550-wmac")) {
3422 id
->vendor_id
= 0x168c;
3423 id
->device_id
= 0x0033;
3424 id
->subsystem_vendor_id
= 0x168c;
3425 id
->subsystem_device_id
= 0x9550;
3426 } else if (!strcmp(compat
, "qca,qca9560-wmac")) {
3427 id
->vendor_id
= 0x168c;
3428 id
->device_id
= 0x0033;
3429 id
->subsystem_vendor_id
= 0x168c;
3430 id
->subsystem_device_id
= 0x9560;
3431 } else if (!strcmp(compat
, "qcom,ipq4019-wifi")) {
3432 id
->vendor_id
= 0x168c;
3433 id
->device_id
= 0x003c;
3434 id
->subsystem_vendor_id
= 0x168c;
3435 id
->subsystem_device_id
= 0x4019;
3436 } else if (!strcmp(compat
, "mediatek,mt7622-wmac")) {
3437 id
->vendor_id
= 0x14c3;
3438 id
->device_id
= 0x7622;
3439 id
->subsystem_vendor_id
= 0x14c3;
3440 id
->subsystem_device_id
= 0x7622;
3441 } else if (!strcmp(compat
, "mediatek,mt7986-wmac")) {
3442 id
->vendor_id
= 0x14c3;
3443 id
->device_id
= 0x7986;
3444 id
->subsystem_vendor_id
= 0x14c3;
3445 id
->subsystem_device_id
= 0x7986;
3448 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
3452 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3454 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3455 char *phy
, num
[8], path
[PATH_MAX
];
3458 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3459 { "vendor", &id
->vendor_id
},
3460 { "device", &id
->device_id
},
3461 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3462 { "subsystem_device", &id
->subsystem_device_id
}
3465 memset(id
, 0, sizeof(*id
));
3467 /* Try to determine the phy name from the given interface */
3468 phy
= nl80211_ifname2phy(ifname
);
3470 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3472 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3473 phy
? "ieee80211" : "net",
3474 phy
? phy
: ifname
, lookup
[i
].path
);
3476 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3477 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3480 /* Failed to obtain hardware IDs, try FDT */
3481 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3482 if (!nl80211_hardware_id_from_fdt(id
, ifname
))
3485 /* Failed to obtain hardware IDs, search board config */
3486 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3487 return iwinfo_hardware_id_from_mtd(id
);
3492 static const struct iwinfo_hardware_entry
*
3493 nl80211_get_hardware_entry(const char *ifname
)
3495 struct iwinfo_hardware_id id
;
3497 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3500 return iwinfo_hardware(&id
);
3503 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3505 const struct iwinfo_hardware_entry
*hw
;
3507 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3508 sprintf(buf
, "Generic MAC80211");
3510 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3515 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3517 const struct iwinfo_hardware_entry
*hw
;
3519 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3522 *buf
= hw
->txpower_offset
;
3526 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3528 const struct iwinfo_hardware_entry
*hw
;
3530 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3533 *buf
= hw
->frequency_offset
;
3537 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3542 if (!strncmp(section
, "path=", 5))
3543 idx
= nl80211_phy_idx_from_path(section
+ 5);
3544 else if (!strncmp(section
, "macaddr=", 8))
3545 idx
= nl80211_phy_idx_from_macaddr(section
+ 8);
3547 idx
= nl80211_phy_idx_from_uci(section
);
3552 name
= nl80211_phyidx2name(idx
);
3560 static int nl80211_phy_path(const char *phyname
, const char **path
)
3562 if (strchr(phyname
, '/'))
3565 *path
= nl80211_phy_path_str(phyname
);
3572 const struct iwinfo_ops nl80211_ops
= {
3574 .probe
= nl80211_probe
,
3575 .channel
= nl80211_get_channel
,
3576 .center_chan1
= nl80211_get_center_chan1
,
3577 .center_chan2
= nl80211_get_center_chan2
,
3578 .frequency
= nl80211_get_frequency
,
3579 .frequency_offset
= nl80211_get_frequency_offset
,
3580 .txpower
= nl80211_get_txpower
,
3581 .txpower_offset
= nl80211_get_txpower_offset
,
3582 .bitrate
= nl80211_get_bitrate
,
3583 .signal
= nl80211_get_signal
,
3584 .noise
= nl80211_get_noise
,
3585 .quality
= nl80211_get_quality
,
3586 .quality_max
= nl80211_get_quality_max
,
3587 .mbssid_support
= nl80211_get_mbssid_support
,
3588 .hwmodelist
= nl80211_get_hwmodelist
,
3589 .htmodelist
= nl80211_get_htmodelist
,
3590 .htmode
= nl80211_get_htmode
,
3591 .mode
= nl80211_get_mode
,
3592 .ssid
= nl80211_get_ssid
,
3593 .bssid
= nl80211_get_bssid
,
3594 .country
= nl80211_get_country
,
3595 .hardware_id
= nl80211_get_hardware_id
,
3596 .hardware_name
= nl80211_get_hardware_name
,
3597 .encryption
= nl80211_get_encryption
,
3598 .phyname
= nl80211_get_phyname
,
3599 .assoclist
= nl80211_get_assoclist
,
3600 .txpwrlist
= nl80211_get_txpwrlist
,
3601 .scanlist
= nl80211_get_scanlist
,
3602 .freqlist
= nl80211_get_freqlist
,
3603 .countrylist
= nl80211_get_countrylist
,
3604 .survey
= nl80211_get_survey
,
3605 .lookup_phy
= nl80211_lookup_phyname
,
3606 .phy_path
= nl80211_phy_path
,
3607 .close
= nl80211_close