2 * iwinfo - Wireless Information Library - NL80211 Backend
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
18 * The signal handling code is derived from the official madwifi tools,
19 * wlanconfig.c in particular. The encryption property handling was
20 * inspired by the hostapd madwifi driver.
22 * Parts of this code are derived from the Linux iw utility.
31 #include "iwinfo_nl80211.h"
33 #define min(x, y) ((x) < (y)) ? (x) : (y)
35 #define BIT(x) (1ULL<<(x))
37 static struct nl80211_state
*nls
= NULL
;
39 static void nl80211_close(void)
44 genl_family_put(nls
->nlctrl
);
47 genl_family_put(nls
->nl80211
);
50 nl_socket_free(nls
->nl_sock
);
53 nl_cache_free(nls
->nl_cache
);
60 static int nl80211_init(void)
66 nls
= malloc(sizeof(struct nl80211_state
));
72 memset(nls
, 0, sizeof(*nls
));
74 nls
->nl_sock
= nl_socket_alloc();
80 if (genl_connect(nls
->nl_sock
)) {
85 fd
= nl_socket_get_fd(nls
->nl_sock
);
86 if (fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
) < 0) {
91 if (genl_ctrl_alloc_cache(nls
->nl_sock
, &nls
->nl_cache
)) {
96 nls
->nl80211
= genl_ctrl_search_by_name(nls
->nl_cache
, "nl80211");
102 nls
->nlctrl
= genl_ctrl_search_by_name(nls
->nl_cache
, "nlctrl");
117 static int nl80211_readint(const char *path
)
123 if ((fd
= open(path
, O_RDONLY
)) > -1)
125 if (read(fd
, buffer
, sizeof(buffer
)) > 0)
134 static int nl80211_readstr(const char *path
, char *buffer
, int length
)
139 if ((fd
= open(path
, O_RDONLY
)) > -1)
141 if ((rv
= read(fd
, buffer
, length
- 1)) > 0)
143 if (buffer
[rv
- 1] == '\n')
156 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
157 struct nlmsgerr
*err
, void *arg
)
164 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
171 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
178 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
183 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
198 static struct nl80211_msg_conveyor
* nl80211_new(struct genl_family
*family
,
201 static struct nl80211_msg_conveyor cv
;
203 struct nl_msg
*req
= NULL
;
204 struct nl_cb
*cb
= NULL
;
210 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
214 genlmsg_put(req
, 0, 0, genl_family_get_id(family
), 0, flags
, cmd
, 0);
228 static struct nl80211_msg_conveyor
* nl80211_ctl(int cmd
, int flags
)
230 if (nl80211_init() < 0)
233 return nl80211_new(nls
->nlctrl
, cmd
, flags
);
236 static const char *nl80211_phy_path_str(const char *phyname
)
238 static char path
[PATH_MAX
];
239 const char *prefix
= "/sys/devices/";
240 int prefix_len
= strlen(prefix
);
243 char buf
[128], *link
;
248 phy_id
= atoi(phyname
+ 3);
249 buf_len
= snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/device", phyname
);
250 link
= realpath(buf
, path
);
254 if (strncmp(link
, prefix
, prefix_len
) != 0)
259 prefix
= "platform/";
260 prefix_len
= strlen(prefix
);
261 if (!strncmp(link
, prefix
, prefix_len
) && strstr(link
, "/pci"))
264 snprintf(buf
+ buf_len
, sizeof(buf
) - buf_len
, "/ieee80211");
269 while ((e
= readdir(d
)) != NULL
) {
272 if (strncmp(e
->d_name
, "phy", 3) != 0)
275 cur_id
= atoi(e
->d_name
+ 3);
276 if (cur_id
>= phy_id
)
287 offset
= link
- path
+ strlen(link
);
288 snprintf(path
+ offset
, sizeof(path
) - offset
, "+%d", seq
);
293 static int nl80211_phy_idx_from_path(const char *path
)
297 const char *cur_path
;
306 path_len
= strlen(path
);
310 d
= opendir("/sys/class/ieee80211");
314 while ((e
= readdir(d
)) != NULL
) {
315 cur_path
= nl80211_phy_path_str(e
->d_name
);
319 cur_path_len
= strlen(cur_path
);
320 if (cur_path_len
< path_len
)
323 if (strcmp(cur_path
+ cur_path_len
- path_len
, path
) != 0)
326 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
327 idx
= nl80211_readint(buf
);
338 static int nl80211_phy_idx_from_macaddr(const char *opt
)
347 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
348 if (glob(buf
, 0, NULL
, &gl
))
351 for (i
= 0; i
< gl
.gl_pathc
; i
++)
353 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
354 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
357 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
360 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
361 if ((idx
= nl80211_readint(buf
)) > -1)
370 static int nl80211_phy_idx_from_phy(const char *opt
)
377 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
378 return nl80211_readint(buf
);
381 static int nl80211_phy_idx_from_uci(const char *name
)
383 struct uci_section
*s
;
387 s
= iwinfo_uci_get_radio(name
, "mac80211");
391 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
392 idx
= nl80211_phy_idx_from_path(opt
);
396 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
397 idx
= nl80211_phy_idx_from_macaddr(opt
);
401 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
402 idx
= nl80211_phy_idx_from_phy(opt
);
409 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
412 unsigned int ifidx
= 0;
414 struct nl80211_msg_conveyor
*cv
;
419 if (nl80211_init() < 0)
422 if (!strncmp(ifname
, "mon.", 4))
423 ifidx
= if_nametoindex(&ifname
[4]);
425 ifidx
= if_nametoindex(ifname
);
428 phyidx
= nl80211_phy_idx_from_phy(ifname
);
430 phyidx
= nl80211_phy_idx_from_uci(ifname
);
433 /* Valid ifidx must be greater than 0 */
434 if ((ifidx
<= 0) && (phyidx
< 0))
437 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
442 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
443 else if (phyidx
> -1)
444 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
453 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
454 int (*cb_func
)(struct nl_msg
*, void *),
457 static struct nl80211_msg_conveyor rcv
;
461 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
463 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
465 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
472 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
473 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
474 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
477 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
484 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
485 int (*cb_func
)(struct nl_msg
*, void *),
488 struct nl80211_msg_conveyor
*cv
;
490 cv
= nl80211_msg(ifname
, cmd
, flags
);
495 return nl80211_send(cv
, cb_func
, cb_arg
);
498 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
500 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
501 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
503 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
504 genlmsg_attrlen(gnlh
, 0), NULL
);
509 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
511 uint32_t *features
= arg
;
512 struct nlattr
**attr
= nl80211_parse(msg
);
514 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
515 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
520 static int nl80211_get_protocol_features(const char *ifname
)
522 struct nl80211_msg_conveyor
*req
;
523 uint32_t features
= 0;
525 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
527 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
534 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
536 struct nl80211_group_conveyor
*cv
= arg
;
538 struct nlattr
**attr
= nl80211_parse(msg
);
539 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
543 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
546 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
548 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
549 nla_data(mgrp
), nla_len(mgrp
), NULL
);
551 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
552 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
553 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
554 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
556 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
564 static int nl80211_subscribe(const char *family
, const char *group
)
566 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
567 struct nl80211_msg_conveyor
*req
;
570 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
573 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
574 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
579 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
589 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
591 struct nl80211_event_conveyor
*cv
= arg
;
592 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
594 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
595 cv
->recv
= gnlh
->cmd
;
600 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
605 static int __nl80211_wait(const char *family
, const char *group
, ...)
607 struct nl80211_event_conveyor cv
= { };
613 if (nl80211_subscribe(family
, group
))
616 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
621 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
622 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
623 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
627 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
628 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
632 while (!cv
.recv
&& !err
)
633 nl_recvmsgs(nls
->nl_sock
, cb
);
640 #define nl80211_wait(family, group, ...) \
641 __nl80211_wait(family, group, __VA_ARGS__, 0)
644 static int nl80211_freq2channel(int freq
)
648 else if (freq
< 2484)
649 return (freq
- 2407) / 5;
650 else if (freq
>= 4910 && freq
<= 4980)
651 return (freq
- 4000) / 5;
652 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
653 return (freq
- 56160) / 2160;
655 return (freq
- 5000) / 5;
658 static int nl80211_channel2freq(int channel
, const char *band
)
660 if (!band
|| band
[0] != 'a')
664 else if (channel
< 14)
665 return (channel
* 5) + 2407;
667 else if ( strcmp(band
, "ad") == 0)
669 return 56160 + 2160 * channel
;
673 if (channel
>= 182 && channel
<= 196)
674 return (channel
* 5) + 4000;
676 return (channel
* 5) + 5000;
682 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
685 struct nlattr
**attr
= nl80211_parse(msg
);
687 if (attr
[NL80211_ATTR_WIPHY_NAME
])
688 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
689 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
696 static char * nl80211_ifname2phy(const char *ifname
)
698 static char phy
[32] = { 0 };
700 memset(phy
, 0, sizeof(phy
));
702 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
703 nl80211_ifname2phy_cb
, phy
);
705 return phy
[0] ? phy
: NULL
;
708 static char * nl80211_phy2ifname(const char *ifname
)
710 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
712 static char nif
[IFNAMSIZ
] = { 0 };
717 /* Only accept phy name of the form phy%d or radio%d */
721 phyidx
= nl80211_phy_idx_from_phy(ifname
);
723 phyidx
= nl80211_phy_idx_from_uci(ifname
);;
727 memset(nif
, 0, sizeof(nif
));
731 if ((d
= opendir("/sys/class/net")) != NULL
)
733 while ((e
= readdir(d
)) != NULL
)
735 snprintf(buffer
, sizeof(buffer
),
736 "/sys/class/net/%s/phy80211/index", e
->d_name
);
738 if (nl80211_readint(buffer
) == phyidx
)
740 snprintf(buffer
, sizeof(buffer
),
741 "/sys/class/net/%s/ifindex", e
->d_name
);
743 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
744 ((ifidx
< 0) || (cifidx
< ifidx
)))
747 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
756 return nif
[0] ? nif
: NULL
;
759 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
762 struct nlattr
**tb
= nl80211_parse(msg
);
763 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
764 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
765 IWINFO_OPMODE_ADHOC
, /* IBSS */
766 IWINFO_OPMODE_CLIENT
, /* managed */
767 IWINFO_OPMODE_MASTER
, /* AP */
768 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
769 IWINFO_OPMODE_WDS
, /* WDS */
770 IWINFO_OPMODE_MONITOR
, /* monitor */
771 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
772 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
773 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
776 if (tb
[NL80211_ATTR_IFTYPE
])
777 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
783 static int nl80211_get_mode(const char *ifname
, int *buf
)
787 *buf
= IWINFO_OPMODE_UNKNOWN
;
789 res
= nl80211_phy2ifname(ifname
);
791 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
792 nl80211_get_mode_cb
, buf
);
794 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
797 static int __nl80211_hostapd_query(const char *ifname
, ...)
800 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
801 int len
, mode
, found
= 0, match
= 1;
804 if (nl80211_get_mode(ifname
, &mode
))
807 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
810 phy
= nl80211_ifname2phy(ifname
);
815 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
816 fp
= fopen(buf
, "r");
821 va_start(ap
, ifname
);
823 /* clear all destination buffers */
826 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
828 dest
= va_arg(ap_cur
, char *);
829 len
= va_arg(ap_cur
, int);
831 memset(dest
, 0, len
);
836 /* iterate applicable lines and copy found values into dest buffers */
837 while (fgets(buf
, sizeof(buf
), fp
))
839 key
= strtok(buf
, " =\t\n");
840 val
= strtok(NULL
, "\n");
842 if (!key
|| !val
|| !*key
|| *key
== '#')
845 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
846 match
= !strcmp(ifname
, val
);
853 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
855 dest
= va_arg(ap_cur
, char *);
856 len
= va_arg(ap_cur
, int);
858 if (!strcmp(search
, key
))
860 strncpy(dest
, val
, len
- 1);
876 #define nl80211_hostapd_query(ifname, ...) \
877 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
880 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
883 struct timeval tv
= { 0, 256000 };
888 memset(buf
, 0, blen
);
890 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
893 if (!FD_ISSET(sock
, &rfds
))
896 return recv(sock
, buf
, blen
- 1, 0);
899 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
901 struct sockaddr_un remote
= { 0 };
902 size_t remote_length
, local_length
;
904 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
908 remote
.sun_family
= AF_UNIX
;
909 remote_length
= sizeof(remote
.sun_family
) +
910 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
913 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
919 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
921 remote_length
= sizeof(remote
.sun_family
) +
922 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
924 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
931 local
->sun_family
= AF_UNIX
;
932 local_length
= sizeof(local
->sun_family
) +
933 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
935 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
944 static int __nl80211_wpactl_query(const char *ifname
, ...)
947 struct sockaddr_un local
= { 0 };
948 int len
, mode
, found
= 0, sock
= -1;
949 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
951 if (nl80211_get_mode(ifname
, &mode
))
954 if (mode
!= IWINFO_OPMODE_CLIENT
&&
955 mode
!= IWINFO_OPMODE_ADHOC
&&
956 mode
!= IWINFO_OPMODE_MESHPOINT
)
959 sock
= nl80211_wpactl_connect(ifname
, &local
);
964 va_start(ap
, ifname
);
966 /* clear all destination buffers */
969 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
971 dest
= va_arg(ap_cur
, char *);
972 len
= va_arg(ap_cur
, int);
974 memset(dest
, 0, len
);
979 send(sock
, "STATUS", 6, 0);
983 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
989 for (line
= strtok_r(buf
, "\n", &pos
);
991 line
= strtok_r(NULL
, "\n", &pos
))
993 key
= strtok(line
, "=");
994 val
= strtok(NULL
, "\n");
1001 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
1003 dest
= va_arg(ap_cur
, char *);
1004 len
= va_arg(ap_cur
, int);
1006 if (!strcmp(search
, key
))
1008 strncpy(dest
, val
, len
- 1);
1023 unlink(local
.sun_path
);
1028 #define nl80211_wpactl_query(ifname, ...) \
1029 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1032 static char * nl80211_ifadd(const char *ifname
)
1034 char path
[PATH_MAX
];
1035 static char nif
[IFNAMSIZ
] = { 0 };
1036 struct nl80211_msg_conveyor
*req
;
1039 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
1042 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
1044 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
1045 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
1047 nl80211_send(req
, NULL
, NULL
);
1049 snprintf(path
, sizeof(path
) - 1,
1050 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
1052 if ((sysfs
= fopen(path
, "w")) != NULL
)
1054 fwrite("0\n", 1, 2, sysfs
);
1067 static void nl80211_ifdel(const char *ifname
)
1069 struct nl80211_msg_conveyor
*req
;
1072 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1075 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1077 nl80211_send(req
, NULL
, NULL
);
1085 static void nl80211_hostapd_hup(const char *ifname
)
1089 char *phy
= nl80211_ifname2phy(ifname
);
1093 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1094 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1096 if (read(fd
, buf
, sizeof(buf
)) > 0)
1108 static int nl80211_probe(const char *ifname
)
1110 return !!nl80211_ifname2phy(ifname
);
1113 struct nl80211_ssid_bssid
{
1114 unsigned char *ssid
;
1115 unsigned char bssid
[7];
1118 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1120 struct nl80211_ssid_bssid
*sb
= arg
;
1121 struct nlattr
**tb
= nl80211_parse(msg
);
1123 if (tb
[NL80211_ATTR_MAC
]) {
1125 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1126 sizeof(sb
->bssid
) - 1);
1132 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1136 struct nl80211_ssid_bssid
*sb
= arg
;
1137 struct nlattr
**tb
= nl80211_parse(msg
);
1138 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1140 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1141 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1142 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1145 if (!tb
[NL80211_ATTR_BSS
] ||
1146 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1148 !bss
[NL80211_BSS_BSSID
] ||
1149 !bss
[NL80211_BSS_STATUS
] ||
1150 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1155 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1157 case NL80211_BSS_STATUS_ASSOCIATED
:
1158 case NL80211_BSS_STATUS_AUTHENTICATED
:
1159 case NL80211_BSS_STATUS_IBSS_JOINED
:
1163 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1164 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1166 while (ielen
>= 2 && ielen
>= ie
[1])
1170 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1181 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1190 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1193 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1195 /* try to find ssid from scan dump results */
1196 res
= nl80211_phy2ifname(ifname
);
1199 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1200 nl80211_get_ssid_bssid_cb
, &sb
);
1202 /* failed, try to find from hostapd info */
1203 if (sb
.ssid
[0] == 0)
1204 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1205 IWINFO_ESSID_MAX_SIZE
+ 1);
1207 /* failed, try to obtain Mesh ID */
1208 if (sb
.ssid
[0] == 0)
1209 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1210 sb
.ssid
, IWINFO_ESSID_MAX_SIZE
+ 1);
1212 return (sb
.ssid
[0] == 0) ? -1 : 0;
1215 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1217 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1218 struct nl80211_ssid_bssid sb
= { };
1220 res
= nl80211_phy2ifname(ifname
);
1222 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1223 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1224 nl80211_get_macaddr_cb
, &sb
);
1226 /* failed, try to find bssid from scan dump results */
1227 if (sb
.bssid
[0] == 0)
1228 nl80211_request(res
? res
: ifname
,
1229 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1230 nl80211_get_ssid_bssid_cb
, &sb
);
1232 /* failed, try to find mac from hostapd info */
1233 if ((sb
.bssid
[0] == 0) &&
1234 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1237 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1238 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1239 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1240 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1241 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1242 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1247 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1248 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1249 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1258 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1261 struct nlattr
**attr
= nl80211_parse(msg
);
1262 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1264 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1265 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1266 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1269 if (attr
[NL80211_ATTR_BSS
] &&
1270 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1271 attr
[NL80211_ATTR_BSS
], bss_policy
))
1273 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1274 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1280 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1283 struct nlattr
**tb
= nl80211_parse(msg
);
1285 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1286 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1291 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1293 char *res
, channel
[4], hwmode
[3];
1295 /* try to find frequency from interface info */
1296 res
= nl80211_phy2ifname(ifname
);
1299 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1300 nl80211_get_frequency_info_cb
, buf
);
1302 /* failed, try to find frequency from hostapd info */
1304 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1305 "channel", channel
, sizeof(channel
)) == 2)
1307 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1310 /* failed, try to find frequency from scan results */
1313 res
= nl80211_phy2ifname(ifname
);
1315 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1316 nl80211_get_frequency_scan_cb
, buf
);
1319 return (*buf
== 0) ? -1 : 0;
1322 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1325 struct nlattr
**tb
= nl80211_parse(msg
);
1327 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1328 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1333 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1337 /* try to find frequency from interface info */
1338 res
= nl80211_phy2ifname(ifname
);
1341 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1342 nl80211_get_center_freq1_cb
, buf
);
1344 return (*buf
== 0) ? -1 : 0;
1347 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1350 struct nlattr
**tb
= nl80211_parse(msg
);
1352 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1353 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1358 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1362 /* try to find frequency from interface info */
1363 res
= nl80211_phy2ifname(ifname
);
1366 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1367 nl80211_get_center_freq2_cb
, buf
);
1369 return (*buf
== 0) ? -1 : 0;
1372 static int nl80211_get_channel(const char *ifname
, int *buf
)
1374 if (!nl80211_get_frequency(ifname
, buf
))
1376 *buf
= nl80211_freq2channel(*buf
);
1383 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1385 if (!nl80211_get_center_freq1(ifname
, buf
))
1387 *buf
= nl80211_freq2channel(*buf
);
1394 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1396 if (!nl80211_get_center_freq2(ifname
, buf
))
1398 *buf
= nl80211_freq2channel(*buf
);
1405 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1408 struct nlattr
**tb
= nl80211_parse(msg
);
1410 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1411 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1416 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1420 res
= nl80211_phy2ifname(ifname
);
1423 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1424 nl80211_get_txpower_cb
, buf
))
1431 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1435 struct nl80211_rssi_rate
*rr
= arg
;
1436 struct nlattr
**attr
= nl80211_parse(msg
);
1437 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1438 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1440 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1441 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1442 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1443 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1444 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1445 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1446 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1447 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1448 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1449 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1450 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1453 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1454 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1455 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1456 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1457 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1460 if (attr
[NL80211_ATTR_STA_INFO
])
1462 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1463 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1465 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1467 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1468 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1472 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1474 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1475 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1478 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1480 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1481 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1492 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1497 memset(r
, 0, sizeof(*r
));
1499 if ((d
= opendir("/sys/class/net")) != NULL
)
1501 while ((de
= readdir(d
)) != NULL
)
1503 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1504 (!de
->d_name
[strlen(ifname
)] ||
1505 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1507 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1508 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1516 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1518 struct nl80211_rssi_rate rr
;
1520 nl80211_fill_signal(ifname
, &rr
);
1522 if (rr
.rate_samples
)
1524 *buf
= (rr
.rate
* 100);
1531 static int nl80211_get_signal(const char *ifname
, int *buf
)
1533 struct nl80211_rssi_rate rr
;
1535 nl80211_fill_signal(ifname
, &rr
);
1537 if (rr
.rssi_samples
)
1546 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1548 int8_t *noise
= arg
;
1549 struct nlattr
**tb
= nl80211_parse(msg
);
1550 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1552 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1553 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1554 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1557 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1560 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1561 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1564 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1567 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1568 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1574 static int nl80211_get_noise(const char *ifname
, int *buf
)
1578 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1579 nl80211_get_noise_cb
, &noise
))
1590 static int nl80211_get_quality(const char *ifname
, int *buf
)
1594 if (!nl80211_get_signal(ifname
, &signal
))
1596 /* A positive signal level is usually just a quality
1597 * value, pass through as-is */
1603 /* The cfg80211 wext compat layer assumes a signal range
1604 * of -110 dBm to -40 dBm, the quality value is derived
1605 * by adding 110 to the signal level */
1610 else if (signal
> -40)
1613 *buf
= (signal
+ 110);
1622 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1624 /* The cfg80211 wext compat layer assumes a maximum
1631 static int nl80211_check_wepkey(const char *key
)
1635 switch (strlen(key
))
1639 return IWINFO_CIPHER_WEP40
;
1643 return IWINFO_CIPHER_WEP104
;
1654 } wpa_key_mgmt_strings
[] = {
1655 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1656 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1657 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1658 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1659 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1660 { "NONE", 0, IWINFO_KMGMT_NONE
},
1661 { "None", 0, IWINFO_KMGMT_NONE
},
1662 { "PSK", 0, IWINFO_KMGMT_PSK
},
1663 { "EAP", 0, IWINFO_KMGMT_8021x
},
1664 { "SAE", 4, IWINFO_KMGMT_SAE
},
1665 { "OWE", 4, IWINFO_KMGMT_OWE
}
1668 static void parse_wpa_suites(const char *str
, int defversion
,
1669 uint8_t *versions
, uint8_t *suites
)
1673 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1679 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1681 m
= wpa_key_mgmt_strings
[i
].match
;
1684 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1686 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1687 version
= wpa_key_mgmt_strings
[i
].version
;
1689 version
= defversion
;
1691 *versions
|= version
;
1692 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1700 q
+= strcspn(q
, sep
);
1702 p
= q
+ strspn(q
, sep
);
1709 } wpa_cipher_strings
[] = {
1710 { "WEP-104", IWINFO_CIPHER_WEP104
},
1711 { "WEP-40", IWINFO_CIPHER_WEP40
},
1712 { "NONE", IWINFO_CIPHER_NONE
},
1713 { "TKIP", IWINFO_CIPHER_TKIP
},
1714 { "CCMP-256",IWINFO_CIPHER_CCMP256
},
1715 { "CCMP", IWINFO_CIPHER_CCMP
},
1716 { "GCMP-256",IWINFO_CIPHER_GCMP256
},
1717 { "GCMP", IWINFO_CIPHER_GCMP
}
1720 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1724 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1730 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1732 m
= wpa_cipher_strings
[i
].match
;
1735 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1737 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1745 q
+= strcspn(q
, sep
);
1747 p
= q
+ strspn(q
, sep
);
1751 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1755 uint8_t wpa_version
= 0;
1756 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1757 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1760 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1762 /* WPA supplicant */
1763 if (nl80211_wpactl_query(ifname
,
1764 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1765 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1766 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1767 "mode", mode
, sizeof(mode
)))
1770 if (!strcmp(wpa_key_mgmt
, "NONE"))
1772 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1773 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1775 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1777 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1778 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1781 c
->pair_ciphers
= 0;
1782 c
->group_ciphers
= 0;
1787 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1791 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1792 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1793 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1799 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1800 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1804 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1809 else if (!strncmp(p
, "WPA-", 4))
1815 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1817 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1824 else if (nl80211_hostapd_query(ifname
,
1825 "wpa", wpa
, sizeof(wpa
),
1826 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1827 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1828 "auth_algs", auth_algs
, sizeof(auth_algs
),
1829 "wep_key0", wep_key0
, sizeof(wep_key0
),
1830 "wep_key1", wep_key1
, sizeof(wep_key1
),
1831 "wep_key2", wep_key2
, sizeof(wep_key2
),
1832 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1836 if (wpa_key_mgmt
[0])
1838 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1840 if (!strncmp(p
, "WPA-", 4))
1843 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1846 c
->enabled
= c
->wpa_version
? 1 : 0;
1849 if (wpa_pairwise
[0])
1850 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1854 switch (atoi(auth_algs
))
1857 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1861 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1865 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1866 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1870 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1871 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1872 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1873 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1875 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1878 c
->group_ciphers
= c
->pair_ciphers
;
1883 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1884 else if (!nl80211_get_mode(ifname
, &opmode
) &&
1885 (opmode
== IWINFO_OPMODE_ADHOC
||
1886 opmode
== IWINFO_OPMODE_MESHPOINT
))
1897 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1901 name
= nl80211_ifname2phy(ifname
);
1908 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1910 name
= nl80211_ifname2phy(name
);
1914 strcpy(buf
, ifname
);
1923 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1924 struct iwinfo_rate_entry
*re
)
1926 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1927 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1928 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1929 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1931 if (ri
[NL80211_RATE_INFO_HE_MCS
])
1934 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_MCS
]);
1936 if (ri
[NL80211_RATE_INFO_HE_NSS
])
1937 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_NSS
]);
1938 if (ri
[NL80211_RATE_INFO_HE_GI
])
1939 re
->he_gi
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_GI
]);
1940 if (ri
[NL80211_RATE_INFO_HE_DCM
])
1941 re
->he_dcm
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_DCM
]);
1943 else if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1946 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1948 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1949 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1951 else if (ri
[NL80211_RATE_INFO_MCS
])
1954 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1957 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1959 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1961 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1963 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1965 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1966 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
1971 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
1972 re
->is_short_gi
= 1;
1974 re
->is_40mhz
= (re
->mhz
== 40);
1977 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
1979 struct nl80211_array_buf
*arr
= arg
;
1980 struct iwinfo_survey_entry
*e
= arr
->buf
;
1981 struct nlattr
**attr
= nl80211_parse(msg
);
1982 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
1985 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1986 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1987 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1988 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
1989 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
1990 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
1991 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
1992 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
1995 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
1996 attr
[NL80211_ATTR_SURVEY_INFO
],
2001 /* advance to end of array */
2003 memset(e
, 0, sizeof(*e
));
2005 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
2006 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
2008 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
2009 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
2011 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
2012 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
2014 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
2015 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
2017 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
2018 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
2020 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
2021 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
2023 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
2024 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
2031 static void plink_state_to_str(char *dst
, unsigned state
)
2034 case NL80211_PLINK_LISTEN
:
2035 strcpy(dst
, "LISTEN");
2037 case NL80211_PLINK_OPN_SNT
:
2038 strcpy(dst
, "OPN_SNT");
2040 case NL80211_PLINK_OPN_RCVD
:
2041 strcpy(dst
, "OPN_RCVD");
2043 case NL80211_PLINK_CNF_RCVD
:
2044 strcpy(dst
, "CNF_RCVD");
2046 case NL80211_PLINK_ESTAB
:
2047 strcpy(dst
, "ESTAB");
2049 case NL80211_PLINK_HOLDING
:
2050 strcpy(dst
, "HOLDING");
2052 case NL80211_PLINK_BLOCKED
:
2053 strcpy(dst
, "BLOCKED");
2056 strcpy(dst
, "UNKNOWN");
2061 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
2063 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
2066 case NL80211_MESH_POWER_ACTIVE
:
2067 strcpy(dst
, "ACTIVE");
2069 case NL80211_MESH_POWER_LIGHT_SLEEP
:
2070 strcpy(dst
, "LIGHT SLEEP");
2072 case NL80211_MESH_POWER_DEEP_SLEEP
:
2073 strcpy(dst
, "DEEP SLEEP");
2076 strcpy(dst
, "UNKNOWN");
2081 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2083 struct nl80211_array_buf
*arr
= arg
;
2084 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2085 struct nlattr
**attr
= nl80211_parse(msg
);
2086 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2087 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2088 struct nl80211_sta_flag_update
*sta_flags
;
2090 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2091 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2092 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2093 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2094 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2095 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2096 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2097 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2098 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2099 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2100 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2101 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2102 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2103 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2104 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2105 [NL80211_STA_INFO_STA_FLAGS
] =
2106 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2107 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2109 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2110 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2111 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2112 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2113 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2114 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2117 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2118 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2119 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2120 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2121 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2124 /* advance to end of array */
2126 memset(e
, 0, sizeof(*e
));
2128 if (attr
[NL80211_ATTR_MAC
])
2129 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2131 if (attr
[NL80211_ATTR_STA_INFO
] &&
2132 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2133 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2135 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2136 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2138 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2139 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2141 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2142 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2144 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2145 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2147 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2148 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2150 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2151 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2153 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2154 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2155 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2156 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2158 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2159 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2160 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2161 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2163 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2164 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2166 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2167 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2169 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2170 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2172 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2173 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2175 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2176 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2178 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2179 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2181 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2182 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2185 if (sinfo
[NL80211_STA_INFO_LLID
])
2186 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2188 if (sinfo
[NL80211_STA_INFO_PLID
])
2189 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2191 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2192 plink_state_to_str(e
->plink_state
,
2193 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2195 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2196 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2197 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2198 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2199 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2200 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2203 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2205 sta_flags
= (struct nl80211_sta_flag_update
*)
2206 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2208 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2209 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2210 e
->is_authorized
= 1;
2212 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2213 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2214 e
->is_authenticated
= 1;
2216 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2217 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2218 e
->is_preamble_short
= 1;
2220 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2221 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2224 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2225 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2228 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2229 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2234 e
->noise
= 0; /* filled in by caller */
2240 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2242 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2245 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2246 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2248 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2255 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2260 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2261 struct iwinfo_assoclist_entry
*e
;
2263 if ((d
= opendir("/sys/class/net")) != NULL
)
2265 while ((de
= readdir(d
)) != NULL
)
2267 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2268 (!de
->d_name
[strlen(ifname
)] ||
2269 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2271 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2272 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2278 if (!nl80211_get_noise(ifname
, &noise
))
2279 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2282 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2289 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2292 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2294 struct nlattr
**attr
= nl80211_parse(msg
);
2295 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2296 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2297 struct nlattr
*band
, *freq
;
2299 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2300 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2301 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2302 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2303 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2304 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2305 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2308 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2311 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2313 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2314 nla_len(band
), NULL
);
2316 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2318 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2319 nla_data(freq
), nla_len(freq
), freq_policy
);
2321 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2322 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2324 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2325 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2327 *dbm_max
= (int)(0.01 * nla_get_u32(
2328 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2338 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2341 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2342 struct nl80211_msg_conveyor
*req
;
2343 struct iwinfo_txpwrlist_entry entry
;
2345 if (nl80211_get_channel(ifname
, &ch_cur
))
2348 /* initialize the value pointer with channel for callback */
2351 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2352 nl80211_get_txpwrlist_cb
, &dbm_max
);
2356 for (dbm_cur
= 0, dbm_cnt
= 0;
2358 dbm_cur
++, dbm_cnt
++)
2360 entry
.dbm
= dbm_cur
;
2361 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2363 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2366 entry
.dbm
= dbm_max
;
2367 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2369 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2372 *len
= dbm_cnt
* sizeof(entry
);
2379 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2381 int wpa_version
= 0;
2382 char *p
, *q
, *proto
, *suites
;
2386 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2387 if (!strcmp(p
, "WEP")) {
2389 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2390 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2391 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2395 proto
= strtok(p
, "-");
2396 suites
= strtok(NULL
, "]");
2398 if (!proto
|| !suites
)
2401 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2403 else if (!strcmp(proto
, "WPA"))
2410 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2411 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2416 struct nl80211_scanlist
{
2417 struct iwinfo_scanlist_entry
*e
;
2422 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2423 struct iwinfo_scanlist_entry
*e
)
2425 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2426 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2427 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2430 while (ielen
>= 2 && ielen
>= ie
[1])
2435 case 114: /* Mesh ID */
2436 if (e
->ssid
[0] == 0) {
2437 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2438 memcpy(e
->ssid
, ie
+ 2, len
);
2444 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2445 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2448 case 221: /* Vendor */
2449 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2450 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2451 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2453 case 61: /* HT oeration */
2455 e
->ht_chan_info
.primary_chan
= ie
[2];
2456 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2457 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2460 case 192: /* VHT operation */
2462 e
->vht_chan_info
.chan_width
= ie
[2];
2463 e
->vht_chan_info
.center_chan_1
= ie
[3];
2464 e
->vht_chan_info
.center_chan_2
= ie
[4];
2474 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2479 struct nl80211_scanlist
*sl
= arg
;
2480 struct nlattr
**tb
= nl80211_parse(msg
);
2481 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2483 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2484 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2485 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2486 [NL80211_BSS_BSSID
] = { 0 },
2487 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2488 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2489 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2490 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2491 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2492 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2493 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2494 [NL80211_BSS_BEACON_IES
] = { 0 },
2497 if (!tb
[NL80211_ATTR_BSS
] ||
2498 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2500 !bss
[NL80211_BSS_BSSID
])
2505 if (bss
[NL80211_BSS_CAPABILITY
])
2506 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2510 memset(sl
->e
, 0, sizeof(*sl
->e
));
2511 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2514 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2515 else if (caps
& (1<<0))
2516 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2518 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2521 sl
->e
->crypto
.enabled
= 1;
2523 if (bss
[NL80211_BSS_FREQUENCY
])
2524 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2525 bss
[NL80211_BSS_FREQUENCY
]));
2527 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2528 nl80211_get_scanlist_ie(bss
, sl
->e
);
2530 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2533 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2535 rssi
= sl
->e
->signal
- 0x100;
2539 else if (rssi
> -40)
2542 sl
->e
->quality
= (rssi
+ 110);
2543 sl
->e
->quality_max
= 70;
2546 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2548 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2549 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2558 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2560 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2562 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2565 if (nl80211_wait("nl80211", "scan",
2566 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2569 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2570 nl80211_get_scanlist_cb
, &sl
))
2573 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2581 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2584 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2585 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2591 if (len
+ 1 >= outlen
)
2601 out
[len
++] = '\n'; in
++;
2605 out
[len
++] = '\r'; in
++;
2609 out
[len
++] = '\t'; in
++;
2613 out
[len
++] = '\033'; in
++;
2617 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2618 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2640 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2642 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2643 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2644 struct sockaddr_un local
= { 0 };
2645 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2647 sock
= nl80211_wpactl_connect(ifname
, &local
);
2652 send(sock
, "ATTACH", 6, 0);
2653 send(sock
, "SCAN", 4, 0);
2656 * wait for scan results:
2657 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2658 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2659 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2660 * time to process the results, so try 72 + 2 + 1 times.
2662 for (tries
= 0; tries
< 75; tries
++)
2664 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2667 /* got an event notification */
2668 if (reply
[0] == '<')
2670 /* scan results are ready */
2671 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2673 /* send "SCAN_RESULTS" command */
2674 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2678 /* is another unrelated event, retry */
2682 /* scanning already in progress, keep awaiting results */
2683 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2688 /* another failure, abort */
2689 else if (!strncmp(reply
, "FAIL-", 5))
2695 /* receive and parse scan results if the wait above didn't time out */
2696 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2698 /* received an event notification, receive again */
2699 if (reply
[0] == '<')
2702 nl80211_get_quality_max(ifname
, &qmax
);
2704 for (line
= strtok_r(reply
, "\n", &pos
);
2706 line
= strtok_r(NULL
, "\n", &pos
))
2708 /* skip header line */
2715 bssid
= strtok(line
, "\t");
2716 freq
= strtok(NULL
, "\t");
2717 signal
= strtok(NULL
, "\t");
2718 flags
= strtok(NULL
, "\t");
2719 ssid
= strtok(NULL
, "\n");
2721 if (!bssid
|| !freq
|| !signal
|| !flags
)
2725 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2726 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2727 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2728 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2729 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2730 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2734 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2739 if (strstr(flags
, "[MESH]"))
2740 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2741 else if (strstr(flags
, "[IBSS]"))
2742 e
->mode
= IWINFO_OPMODE_ADHOC
;
2744 e
->mode
= IWINFO_OPMODE_MASTER
;
2747 e
->channel
= nl80211_freq2channel(atoi(freq
));
2750 rssi
= atoi(signal
);
2756 /* The cfg80211 wext compat layer assumes a signal range
2757 * of -110 dBm to -40 dBm, the quality value is derived
2758 * by adding 110 to the signal level */
2761 else if (rssi
> -40)
2764 e
->quality
= (rssi
+ 110);
2772 e
->quality_max
= qmax
;
2775 nl80211_get_scancrypto(flags
, &e
->crypto
);
2781 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2786 unlink(local
.sun_path
);
2788 return (count
>= 0) ? 0 : -1;
2791 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2798 /* Got a radioX pseudo interface, find some interface on it or create one */
2799 if (!strncmp(ifname
, "radio", 5))
2801 /* Reuse existing interface */
2802 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2804 return nl80211_get_scanlist(res
, buf
, len
);
2807 /* Need to spawn a temporary iface for scanning */
2808 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2810 rv
= nl80211_get_scanlist(res
, buf
, len
);
2816 /* WPA supplicant */
2817 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2822 /* station / ad-hoc / monitor scan */
2823 else if (!nl80211_get_mode(ifname
, &mode
) &&
2824 (mode
== IWINFO_OPMODE_ADHOC
||
2825 mode
== IWINFO_OPMODE_MASTER
||
2826 mode
== IWINFO_OPMODE_CLIENT
||
2827 mode
== IWINFO_OPMODE_MONITOR
) &&
2828 iwinfo_ifup(ifname
))
2830 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2836 /* Got a temp interface, don't create yet another one */
2837 if (!strncmp(ifname
, "tmp.", 4))
2839 if (!iwinfo_ifup(ifname
))
2842 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2843 iwinfo_ifdown(ifname
);
2847 /* Spawn a new scan interface */
2850 if (!(res
= nl80211_ifadd(ifname
)))
2855 /* if we can take the new interface up, the driver supports an
2856 * additional interface and there's no need to tear down the ap */
2857 if (iwinfo_ifup(res
))
2859 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2863 /* driver cannot create secondary interface, take down ap
2865 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2867 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2869 iwinfo_ifup(ifname
);
2870 nl80211_hostapd_hup(ifname
);
2881 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2883 int bands_remain
, freqs_remain
;
2885 struct nl80211_array_buf
*arr
= arg
;
2886 struct iwinfo_freqlist_entry
*e
;
2888 struct nlattr
**attr
= nl80211_parse(msg
);
2889 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2890 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2891 struct nlattr
*band
, *freq
;
2896 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2897 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2899 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2900 nla_data(band
), nla_len(band
), NULL
);
2902 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2903 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2905 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2906 nla_data(freq
), nla_len(freq
), NULL
);
2908 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2909 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2912 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2913 e
->channel
= nl80211_freq2channel(e
->mhz
);
2916 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2917 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2920 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2921 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2922 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2923 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2924 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2925 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2926 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2927 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2928 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2929 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2930 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2931 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2943 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2945 struct nl80211_msg_conveyor
*cv
;
2946 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2947 uint32_t features
= nl80211_get_protocol_features(ifname
);
2950 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
2951 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
2955 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
2956 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
2959 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
2969 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
2972 struct nlattr
**attr
= nl80211_parse(msg
);
2974 if (attr
[NL80211_ATTR_REG_ALPHA2
])
2975 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
2982 static int nl80211_get_country(const char *ifname
, char *buf
)
2984 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
2985 nl80211_get_country_cb
, buf
))
2991 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
2994 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
2995 const struct iwinfo_iso3166_label
*l
;
2997 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
2999 e
->iso3166
= l
->iso3166
;
3000 e
->ccode
[0] = (l
->iso3166
/ 256);
3001 e
->ccode
[1] = (l
->iso3166
% 256);
3005 *len
= (count
* sizeof(struct iwinfo_country_entry
));
3010 struct nl80211_modes
3020 uint16_t he_phy_cap
[6];
3023 static int nl80211_eval_modelist(struct nl80211_modes
*m
)
3025 /* Treat any nonzero capability as 11n */
3028 m
->hw
|= IWINFO_80211_N
;
3029 m
->ht
|= IWINFO_HTMODE_HT20
;
3031 if (m
->nl_ht
& (1 << 1))
3032 m
->ht
|= IWINFO_HTMODE_HT40
;
3035 if (m
->he_phy_cap
[0] != 0) {
3036 m
->hw
|= IWINFO_80211_AX
;
3037 m
->ht
|= IWINFO_HTMODE_HE20
;
3039 if (m
->he_phy_cap
[0] & BIT(9))
3040 m
->ht
|= IWINFO_HTMODE_HE40
;
3041 if (m
->he_phy_cap
[0] & BIT(10))
3042 m
->ht
|= IWINFO_HTMODE_HE40
| IWINFO_HTMODE_HE80
;
3043 if (m
->he_phy_cap
[0] & BIT(11))
3044 m
->ht
|= IWINFO_HTMODE_HE160
;
3045 if (m
->he_phy_cap
[0] & BIT(12))
3046 m
->ht
|= IWINFO_HTMODE_HE160
| IWINFO_HTMODE_HE80_80
;
3049 if (m
->bands
& IWINFO_BAND_24
)
3051 m
->hw
|= IWINFO_80211_B
;
3052 m
->hw
|= IWINFO_80211_G
;
3055 if (m
->bands
& IWINFO_BAND_5
)
3057 /* Treat any nonzero capability as 11ac */
3060 m
->hw
|= IWINFO_80211_AC
;
3061 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3063 switch ((m
->nl_vht
>> 2) & 3)
3066 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3070 m
->ht
|= IWINFO_HTMODE_VHT160
;
3075 m
->hw
|= IWINFO_80211_A
;
3079 if (m
->bands
& IWINFO_BAND_60
)
3081 m
->hw
|= IWINFO_80211_AD
;
3086 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
3088 struct nl80211_modes
*m
= arg
;
3089 int bands_remain
, freqs_remain
;
3091 uint32_t vht_caps
= 0;
3092 struct nlattr
**attr
= nl80211_parse(msg
);
3093 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3094 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
3095 struct nlattr
*band
, *freq
;
3098 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
3100 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3102 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3103 nla_data(band
), nla_len(band
), NULL
);
3105 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
3106 m
->nl_ht
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
3108 if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
3109 m
->nl_vht
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
3111 if (bands
[NL80211_BAND_ATTR_IFTYPE_DATA
]) {
3112 struct nlattr
*tb
[NL80211_BAND_IFTYPE_ATTR_MAX
+ 1];
3113 struct nlattr
*nl_iftype
;
3117 nla_for_each_nested(nl_iftype
, bands
[NL80211_BAND_ATTR_IFTYPE_DATA
], rem_band
) {
3118 nla_parse(tb
, NL80211_BAND_IFTYPE_ATTR_MAX
,
3119 nla_data(nl_iftype
), nla_len(nl_iftype
), NULL
);
3120 if (tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]) {
3121 len
= nla_len(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]);
3123 if (len
> sizeof(m
->he_phy_cap
) - 1)
3124 len
= sizeof(m
->he_phy_cap
) - 1;
3125 memcpy(&((__u8
*)m
->he_phy_cap
)[1],
3126 nla_data(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]),
3132 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
3133 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
3136 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
3137 nla_data(freq
), nla_len(freq
), NULL
);
3139 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
3142 freq_mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
3144 if (freq_mhz
> 2400 && freq_mhz
< 2485)
3146 m
->bands
|= IWINFO_BAND_24
;
3148 else if (freq_mhz
> 5000 && freq_mhz
< 5850)
3150 m
->bands
|= IWINFO_BAND_5
;
3152 else if (freq_mhz
> 6000 && freq_mhz
< 7120)
3154 m
->bands
|= IWINFO_BAND_6
;
3156 else if (freq_mhz
>= 56160)
3158 m
->bands
|= IWINFO_BAND_60
;
3170 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3172 struct nl80211_msg_conveyor
*cv
;
3173 struct nl80211_modes m
= {};
3174 uint32_t features
= nl80211_get_protocol_features(ifname
);
3177 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3178 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3182 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3183 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3184 goto nla_put_failure
;
3186 nl80211_eval_modelist(&m
);
3203 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3205 struct nlattr
**tb
= nl80211_parse(msg
);
3207 struct chan_info
*chn
= arg
;
3209 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3210 chn
->width
= nla_get_u32(cur
);
3212 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3213 chn
->mode
= nla_get_u32(cur
);
3218 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3220 struct chan_info chn
= { .width
= 0, .mode
= 0 };
3224 res
= nl80211_phy2ifname(ifname
);
3227 err
= nl80211_request(res
? res
: ifname
,
3228 NL80211_CMD_GET_INTERFACE
, 0,
3229 nl80211_get_htmode_cb
, &chn
);
3233 switch (chn
.width
) {
3234 case NL80211_CHAN_WIDTH_20
:
3236 *buf
= IWINFO_HTMODE_VHT20
;
3238 *buf
= IWINFO_HTMODE_HT20
;
3240 case NL80211_CHAN_WIDTH_40
:
3242 *buf
= IWINFO_HTMODE_VHT40
;
3244 *buf
= IWINFO_HTMODE_HT40
;
3246 case NL80211_CHAN_WIDTH_80
:
3247 *buf
= IWINFO_HTMODE_VHT80
;
3249 case NL80211_CHAN_WIDTH_80P80
:
3250 *buf
= IWINFO_HTMODE_VHT80_80
;
3252 case NL80211_CHAN_WIDTH_160
:
3253 *buf
= IWINFO_HTMODE_VHT160
;
3255 case NL80211_CHAN_WIDTH_5
:
3256 case NL80211_CHAN_WIDTH_10
:
3257 case NL80211_CHAN_WIDTH_20_NOHT
:
3258 *buf
= IWINFO_HTMODE_NOHT
;
3267 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3269 struct nl80211_msg_conveyor
*cv
;
3270 struct nl80211_modes m
= {};
3271 uint32_t features
= nl80211_get_protocol_features(ifname
);
3274 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3275 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3279 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3280 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3281 goto nla_put_failure
;
3283 nl80211_eval_modelist(&m
);
3296 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3298 struct nlattr
**attr
= nl80211_parse(msg
);
3299 struct nlattr
*comb
;
3301 int comb_rem
, limit_rem
, mode_rem
;
3304 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3307 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3309 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3310 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3311 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3313 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3314 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3315 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3316 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3318 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3319 struct nlattr
*limit
;
3321 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3323 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3326 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3328 struct nlattr
*mode
;
3330 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3332 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3333 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3336 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3339 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3340 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3349 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3351 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3352 nl80211_get_ifcomb_cb
, buf
))
3358 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3360 char *phy
, compat
[64], path
[PATH_MAX
];
3363 /* Try to determine the phy name from the given interface */
3364 phy
= nl80211_ifname2phy(ifname
);
3366 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3367 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3369 if (nl80211_readstr(path
, compat
, sizeof(compat
)) <= 0)
3372 if (!strcmp(compat
, "qca,ar9130-wmac")) {
3373 id
->vendor_id
= 0x168c;
3374 id
->device_id
= 0x0029;
3375 id
->subsystem_vendor_id
= 0x168c;
3376 id
->subsystem_device_id
= 0x9130;
3377 } else if (!strcmp(compat
, "qca,ar9330-wmac")) {
3378 id
->vendor_id
= 0x168c;
3379 id
->device_id
= 0x0030;
3380 id
->subsystem_vendor_id
= 0x168c;
3381 id
->subsystem_device_id
= 0x9330;
3382 } else if (!strcmp(compat
, "qca,ar9340-wmac")) {
3383 id
->vendor_id
= 0x168c;
3384 id
->device_id
= 0x0030;
3385 id
->subsystem_vendor_id
= 0x168c;
3386 id
->subsystem_device_id
= 0x9340;
3387 } else if (!strcmp(compat
, "qca,qca9530-wmac")) {
3388 id
->vendor_id
= 0x168c;
3389 id
->device_id
= 0x0033;
3390 id
->subsystem_vendor_id
= 0x168c;
3391 id
->subsystem_device_id
= 0x9530;
3392 } else if (!strcmp(compat
, "qca,qca9550-wmac")) {
3393 id
->vendor_id
= 0x168c;
3394 id
->device_id
= 0x0033;
3395 id
->subsystem_vendor_id
= 0x168c;
3396 id
->subsystem_device_id
= 0x9550;
3397 } else if (!strcmp(compat
, "qca,qca9560-wmac")) {
3398 id
->vendor_id
= 0x168c;
3399 id
->device_id
= 0x0033;
3400 id
->subsystem_vendor_id
= 0x168c;
3401 id
->subsystem_device_id
= 0x9560;
3402 } else if (!strcmp(compat
, "qcom,ipq4019-wifi")) {
3403 id
->vendor_id
= 0x168c;
3404 id
->device_id
= 0x003c;
3405 id
->subsystem_vendor_id
= 0x168c;
3406 id
->subsystem_device_id
= 0x4019;
3407 } else if (!strcmp(compat
, "mediatek,mt7622-wmac")) {
3408 id
->vendor_id
= 0x14c3;
3409 id
->device_id
= 0x7622;
3410 id
->subsystem_vendor_id
= 0x14c3;
3411 id
->subsystem_device_id
= 0x7622;
3412 } else if (!strcmp(compat
, "mediatek,mt7986-wmac")) {
3413 id
->vendor_id
= 0x14c3;
3414 id
->device_id
= 0x7986;
3415 id
->subsystem_vendor_id
= 0x14c3;
3416 id
->subsystem_device_id
= 0x7986;
3419 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
3423 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3425 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3426 char *phy
, num
[8], path
[PATH_MAX
];
3429 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3430 { "vendor", &id
->vendor_id
},
3431 { "device", &id
->device_id
},
3432 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3433 { "subsystem_device", &id
->subsystem_device_id
}
3436 memset(id
, 0, sizeof(*id
));
3438 /* Try to determine the phy name from the given interface */
3439 phy
= nl80211_ifname2phy(ifname
);
3441 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3443 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3444 phy
? "ieee80211" : "net",
3445 phy
? phy
: ifname
, lookup
[i
].path
);
3447 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3448 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3451 /* Failed to obtain hardware IDs, try FDT */
3452 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3453 if (!nl80211_hardware_id_from_fdt(id
, ifname
))
3456 /* Failed to obtain hardware IDs, search board config */
3457 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3458 return iwinfo_hardware_id_from_mtd(id
);
3463 static const struct iwinfo_hardware_entry
*
3464 nl80211_get_hardware_entry(const char *ifname
)
3466 struct iwinfo_hardware_id id
;
3468 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3471 return iwinfo_hardware(&id
);
3474 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3476 const struct iwinfo_hardware_entry
*hw
;
3478 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3479 sprintf(buf
, "Generic MAC80211");
3481 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3486 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3488 const struct iwinfo_hardware_entry
*hw
;
3490 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3493 *buf
= hw
->txpower_offset
;
3497 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3499 const struct iwinfo_hardware_entry
*hw
;
3501 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3504 *buf
= hw
->frequency_offset
;
3508 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3512 if (!strncmp(section
, "path=", 5))
3513 idx
= nl80211_phy_idx_from_path(section
+ 5);
3514 else if (!strncmp(section
, "macaddr=", 8))
3515 idx
= nl80211_phy_idx_from_macaddr(section
+ 8);
3517 idx
= nl80211_phy_idx_from_uci(section
);
3522 sprintf(buf
, "phy%d", idx
);
3526 static int nl80211_phy_path(const char *phyname
, const char **path
)
3528 if (strchr(phyname
, '/'))
3531 *path
= nl80211_phy_path_str(phyname
);
3538 const struct iwinfo_ops nl80211_ops
= {
3540 .probe
= nl80211_probe
,
3541 .channel
= nl80211_get_channel
,
3542 .center_chan1
= nl80211_get_center_chan1
,
3543 .center_chan2
= nl80211_get_center_chan2
,
3544 .frequency
= nl80211_get_frequency
,
3545 .frequency_offset
= nl80211_get_frequency_offset
,
3546 .txpower
= nl80211_get_txpower
,
3547 .txpower_offset
= nl80211_get_txpower_offset
,
3548 .bitrate
= nl80211_get_bitrate
,
3549 .signal
= nl80211_get_signal
,
3550 .noise
= nl80211_get_noise
,
3551 .quality
= nl80211_get_quality
,
3552 .quality_max
= nl80211_get_quality_max
,
3553 .mbssid_support
= nl80211_get_mbssid_support
,
3554 .hwmodelist
= nl80211_get_hwmodelist
,
3555 .htmodelist
= nl80211_get_htmodelist
,
3556 .htmode
= nl80211_get_htmode
,
3557 .mode
= nl80211_get_mode
,
3558 .ssid
= nl80211_get_ssid
,
3559 .bssid
= nl80211_get_bssid
,
3560 .country
= nl80211_get_country
,
3561 .hardware_id
= nl80211_get_hardware_id
,
3562 .hardware_name
= nl80211_get_hardware_name
,
3563 .encryption
= nl80211_get_encryption
,
3564 .phyname
= nl80211_get_phyname
,
3565 .assoclist
= nl80211_get_assoclist
,
3566 .txpwrlist
= nl80211_get_txpwrlist
,
3567 .scanlist
= nl80211_get_scanlist
,
3568 .freqlist
= nl80211_get_freqlist
,
3569 .countrylist
= nl80211_get_countrylist
,
3570 .survey
= nl80211_get_survey
,
3571 .lookup_phy
= nl80211_lookup_phyname
,
3572 .phy_path
= nl80211_phy_path
,
3573 .close
= nl80211_close