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.
32 #include "iwinfo_nl80211.h"
34 #define min(x, y) ((x) < (y)) ? (x) : (y)
36 #define BIT(x) (1ULL<<(x))
38 static struct nl80211_state
*nls
= NULL
;
40 static void nl80211_close(void)
45 genl_family_put(nls
->nlctrl
);
48 genl_family_put(nls
->nl80211
);
51 nl_socket_free(nls
->nl_sock
);
54 nl_cache_free(nls
->nl_cache
);
61 static int nl80211_init(void)
67 nls
= malloc(sizeof(struct nl80211_state
));
73 memset(nls
, 0, sizeof(*nls
));
75 nls
->nl_sock
= nl_socket_alloc();
81 if (genl_connect(nls
->nl_sock
)) {
86 fd
= nl_socket_get_fd(nls
->nl_sock
);
87 if (fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
) < 0) {
92 if (genl_ctrl_alloc_cache(nls
->nl_sock
, &nls
->nl_cache
)) {
97 nls
->nl80211
= genl_ctrl_search_by_name(nls
->nl_cache
, "nl80211");
103 nls
->nlctrl
= genl_ctrl_search_by_name(nls
->nl_cache
, "nlctrl");
118 static int nl80211_readint(const char *path
)
124 if ((fd
= open(path
, O_RDONLY
)) > -1)
126 if (read(fd
, buffer
, sizeof(buffer
)) > 0)
135 static int nl80211_readstr(const char *path
, char *buffer
, int length
)
140 if ((fd
= open(path
, O_RDONLY
)) > -1)
142 if ((rv
= read(fd
, buffer
, length
- 1)) > 0)
144 if (buffer
[rv
- 1] == '\n')
157 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
158 struct nlmsgerr
*err
, void *arg
)
165 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
172 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
179 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
184 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
199 static struct nl80211_msg_conveyor
* nl80211_new(struct genl_family
*family
,
202 static struct nl80211_msg_conveyor cv
;
204 struct nl_msg
*req
= NULL
;
205 struct nl_cb
*cb
= NULL
;
211 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
215 genlmsg_put(req
, 0, 0, genl_family_get_id(family
), 0, flags
, cmd
, 0);
229 static struct nl80211_msg_conveyor
* nl80211_ctl(int cmd
, int flags
)
231 if (nl80211_init() < 0)
234 return nl80211_new(nls
->nlctrl
, cmd
, flags
);
237 static const char *nl80211_phy_path_str(const char *phyname
)
239 static char path
[PATH_MAX
];
240 const char *prefix
= "/sys/devices/";
241 int prefix_len
= strlen(prefix
);
244 char buf
[512], *link
;
249 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", phyname
);
250 phy_idx
= nl80211_readint(buf
);
254 buf_len
= snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/device", phyname
);
255 link
= realpath(buf
, path
);
259 if (strncmp(link
, prefix
, prefix_len
) != 0)
264 prefix
= "platform/";
265 prefix_len
= strlen(prefix
);
266 if (!strncmp(link
, prefix
, prefix_len
) && strstr(link
, "/pci"))
269 snprintf(buf
+ buf_len
, sizeof(buf
) - buf_len
, "/ieee80211");
274 while ((e
= readdir(d
)) != NULL
) {
277 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
278 cur_idx
= nl80211_readint(buf
);
282 if (cur_idx
>= phy_idx
)
293 offset
= link
- path
+ strlen(link
);
294 snprintf(path
+ offset
, sizeof(path
) - offset
, "+%d", seq
);
299 static int nl80211_phy_idx_from_path(const char *path
)
303 const char *cur_path
;
312 path_len
= strlen(path
);
316 d
= opendir("/sys/class/ieee80211");
320 while ((e
= readdir(d
)) != NULL
) {
321 cur_path
= nl80211_phy_path_str(e
->d_name
);
325 cur_path_len
= strlen(cur_path
);
326 if (cur_path_len
< path_len
)
329 if (strcmp(cur_path
+ cur_path_len
- path_len
, path
) != 0)
332 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
333 idx
= nl80211_readint(buf
);
344 static int nl80211_phy_idx_from_macaddr(const char *opt
)
353 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
354 if (glob(buf
, 0, NULL
, &gl
))
357 for (i
= 0; i
< gl
.gl_pathc
; i
++)
359 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
360 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
363 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
366 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
367 if ((idx
= nl80211_readint(buf
)) > -1)
376 static int nl80211_phy_idx_from_phy(const char *opt
)
383 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
384 return nl80211_readint(buf
);
387 static int nl80211_phy_idx_from_uci(const char *name
)
389 struct uci_section
*s
;
393 s
= iwinfo_uci_get_radio(name
, "mac80211");
397 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
398 idx
= nl80211_phy_idx_from_path(opt
);
402 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
403 idx
= nl80211_phy_idx_from_macaddr(opt
);
407 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
408 idx
= nl80211_phy_idx_from_phy(opt
);
415 static bool nl80211_is_ifname(const char *name
)
418 char buffer
[PATH_MAX
];
420 snprintf(buffer
, sizeof(buffer
), "/sys/class/net/%s", name
);
421 return !lstat(buffer
, &st
);
424 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
427 unsigned int ifidx
= 0;
429 struct nl80211_msg_conveyor
*cv
;
434 if (nl80211_init() < 0)
437 if (!strncmp(ifname
, "mon.", 4))
438 ifidx
= if_nametoindex(&ifname
[4]);
439 else if (nl80211_is_ifname(ifname
))
440 ifidx
= if_nametoindex(ifname
);
443 phyidx
= nl80211_phy_idx_from_phy(ifname
);
445 phyidx
= nl80211_phy_idx_from_uci(ifname
);
448 /* Valid ifidx must be greater than 0 */
449 if ((ifidx
<= 0) && (phyidx
< 0))
452 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
457 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
458 else if (phyidx
> -1)
459 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
468 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
469 int (*cb_func
)(struct nl_msg
*, void *),
472 static struct nl80211_msg_conveyor rcv
;
476 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
478 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
480 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
487 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
488 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
489 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
492 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
499 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
500 int (*cb_func
)(struct nl_msg
*, void *),
503 struct nl80211_msg_conveyor
*cv
;
505 cv
= nl80211_msg(ifname
, cmd
, flags
);
510 return nl80211_send(cv
, cb_func
, cb_arg
);
513 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
515 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
516 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
518 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
519 genlmsg_attrlen(gnlh
, 0), NULL
);
524 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
526 uint32_t *features
= arg
;
527 struct nlattr
**attr
= nl80211_parse(msg
);
529 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
530 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
535 static int nl80211_get_protocol_features(const char *ifname
)
537 struct nl80211_msg_conveyor
*req
;
538 uint32_t features
= 0;
540 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
542 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
549 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
551 struct nl80211_group_conveyor
*cv
= arg
;
553 struct nlattr
**attr
= nl80211_parse(msg
);
554 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
558 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
561 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
563 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
564 nla_data(mgrp
), nla_len(mgrp
), NULL
);
566 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
567 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
568 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
569 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
571 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
579 static int nl80211_subscribe(const char *family
, const char *group
)
581 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
582 struct nl80211_msg_conveyor
*req
;
585 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
588 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
589 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
594 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
604 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
606 struct nl80211_event_conveyor
*cv
= arg
;
607 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
609 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
610 cv
->recv
= gnlh
->cmd
;
615 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
620 static int __nl80211_wait(const char *family
, const char *group
, ...)
622 struct nl80211_event_conveyor cv
= { };
628 if (nl80211_subscribe(family
, group
))
631 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
636 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
637 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
638 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
642 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
643 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
647 while (!cv
.recv
&& !err
)
648 nl_recvmsgs(nls
->nl_sock
, cb
);
655 #define nl80211_wait(family, group, ...) \
656 __nl80211_wait(family, group, __VA_ARGS__, 0)
659 static int nl80211_freq2channel(int freq
)
663 else if (freq
< 2484)
664 return (freq
- 2407) / 5;
665 else if (freq
>= 4910 && freq
<= 4980)
666 return (freq
- 4000) / 5;
667 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
668 return (freq
- 56160) / 2160;
670 return (freq
- 5000) / 5;
673 static int nl80211_channel2freq(int channel
, const char *band
)
675 if (!band
|| band
[0] != 'a')
679 else if (channel
< 14)
680 return (channel
* 5) + 2407;
682 else if ( strcmp(band
, "ad") == 0)
684 return 56160 + 2160 * channel
;
688 if (channel
>= 182 && channel
<= 196)
689 return (channel
* 5) + 4000;
691 return (channel
* 5) + 5000;
697 static int nl80211_phyname_cb(struct nl_msg
*msg
, void *arg
)
700 struct nlattr
**attr
= nl80211_parse(msg
);
702 if (attr
[NL80211_ATTR_WIPHY_NAME
])
703 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
704 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
711 static char * nl80211_ifname2phy(const char *ifname
)
713 static char phy
[32] = { 0 };
715 memset(phy
, 0, sizeof(phy
));
717 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
718 nl80211_phyname_cb
, phy
);
720 return phy
[0] ? phy
: NULL
;
723 static char * nl80211_phyidx2name(unsigned int idx
)
725 struct nl80211_msg_conveyor
*cv
;
726 static char phy
[32] = { 0 };
728 if (nl80211_init() < 0)
731 cv
= nl80211_new(nls
->nl80211
, NL80211_CMD_GET_WIPHY
, 0);
735 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, idx
);
737 memset(phy
, 0, sizeof(phy
));
738 nl80211_send(cv
, nl80211_phyname_cb
, phy
);
740 return phy
[0] ? phy
: NULL
;
746 static char * nl80211_phy2ifname(const char *ifname
)
748 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
750 static char nif
[IFNAMSIZ
] = { 0 };
755 /* Only accept phy name of the form phy%d or radio%d */
759 phyidx
= nl80211_phy_idx_from_phy(ifname
);
761 phyidx
= nl80211_phy_idx_from_uci(ifname
);;
765 memset(nif
, 0, sizeof(nif
));
769 if ((d
= opendir("/sys/class/net")) != NULL
)
771 while ((e
= readdir(d
)) != NULL
)
773 snprintf(buffer
, sizeof(buffer
),
774 "/sys/class/net/%s/phy80211/index", e
->d_name
);
776 if (nl80211_readint(buffer
) == phyidx
)
778 snprintf(buffer
, sizeof(buffer
),
779 "/sys/class/net/%s/ifindex", e
->d_name
);
781 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
782 ((ifidx
< 0) || (cifidx
< ifidx
)))
785 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
794 return nif
[0] ? nif
: NULL
;
797 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
800 struct nlattr
**tb
= nl80211_parse(msg
);
801 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
802 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
803 IWINFO_OPMODE_ADHOC
, /* IBSS */
804 IWINFO_OPMODE_CLIENT
, /* managed */
805 IWINFO_OPMODE_MASTER
, /* AP */
806 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
807 IWINFO_OPMODE_WDS
, /* WDS */
808 IWINFO_OPMODE_MONITOR
, /* monitor */
809 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
810 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
811 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
814 if (tb
[NL80211_ATTR_IFTYPE
])
815 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
821 static int nl80211_get_mode(const char *ifname
, int *buf
)
825 *buf
= IWINFO_OPMODE_UNKNOWN
;
827 res
= nl80211_phy2ifname(ifname
);
829 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
830 nl80211_get_mode_cb
, buf
);
832 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
835 static int __nl80211_hostapd_query(const char *ifname
, ...)
838 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
839 int len
, mode
, found
= 0, match
= 1;
842 if (nl80211_get_mode(ifname
, &mode
))
845 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
848 phy
= nl80211_ifname2phy(ifname
);
853 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
854 fp
= fopen(buf
, "r");
859 va_start(ap
, ifname
);
861 /* clear all destination buffers */
864 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
866 dest
= va_arg(ap_cur
, char *);
867 len
= va_arg(ap_cur
, int);
869 memset(dest
, 0, len
);
874 /* iterate applicable lines and copy found values into dest buffers */
875 while (fgets(buf
, sizeof(buf
), fp
))
877 key
= strtok(buf
, " =\t\n");
878 val
= strtok(NULL
, "\n");
880 if (!key
|| !val
|| !*key
|| *key
== '#')
883 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
884 match
= !strcmp(ifname
, val
);
891 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
893 dest
= va_arg(ap_cur
, char *);
894 len
= va_arg(ap_cur
, int);
896 if (!strcmp(search
, key
))
898 strncpy(dest
, val
, len
- 1);
914 #define nl80211_hostapd_query(ifname, ...) \
915 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
918 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
921 struct timeval tv
= { 0, 256000 };
926 memset(buf
, 0, blen
);
928 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
931 if (!FD_ISSET(sock
, &rfds
))
934 return recv(sock
, buf
, blen
- 1, 0);
937 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
939 struct sockaddr_un remote
= { 0 };
940 size_t remote_length
, local_length
;
942 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
946 remote
.sun_family
= AF_UNIX
;
947 remote_length
= sizeof(remote
.sun_family
) +
948 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
950 /* Set client socket file permissions so that bind() creates the client
951 * socket with these permissions and there is no need to try to change
952 * them with chmod() after bind() which would have potential issues with
953 * race conditions. These permissions are needed to make sure the server
954 * side (wpa_supplicant or hostapd) can reply to the control interface
957 * The lchown() calls below after bind() are also part of the needed
958 * operations to allow the response to go through. Those are using the
959 * no-deference-symlinks version to avoid races. */
960 fchmod(sock
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
962 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
968 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
974 local
->sun_family
= AF_UNIX
;
975 local_length
= sizeof(local
->sun_family
) +
976 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
978 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
984 /* Set group even if we do not have privileges to change owner */
985 lchown(local
->sun_path
, -1, 101);
986 lchown(local
->sun_path
, 101, 101);
991 static int __nl80211_wpactl_query(const char *ifname
, ...)
994 struct sockaddr_un local
= { 0 };
995 int len
, mode
, found
= 0, sock
= -1;
996 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
998 if (nl80211_get_mode(ifname
, &mode
))
1001 if (mode
!= IWINFO_OPMODE_CLIENT
&&
1002 mode
!= IWINFO_OPMODE_ADHOC
&&
1003 mode
!= IWINFO_OPMODE_MESHPOINT
)
1006 sock
= nl80211_wpactl_connect(ifname
, &local
);
1011 va_start(ap
, ifname
);
1013 /* clear all destination buffers */
1014 va_copy(ap_cur
, ap
);
1016 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
1018 dest
= va_arg(ap_cur
, char *);
1019 len
= va_arg(ap_cur
, int);
1021 memset(dest
, 0, len
);
1026 send(sock
, "STATUS", 6, 0);
1030 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
1036 for (line
= strtok_r(buf
, "\n", &pos
);
1038 line
= strtok_r(NULL
, "\n", &pos
))
1040 key
= strtok(line
, "=");
1041 val
= strtok(NULL
, "\n");
1046 va_copy(ap_cur
, ap
);
1048 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
1050 dest
= va_arg(ap_cur
, char *);
1051 len
= va_arg(ap_cur
, int);
1053 if (!strcmp(search
, key
))
1055 strncpy(dest
, val
, len
- 1);
1070 unlink(local
.sun_path
);
1075 #define nl80211_wpactl_query(ifname, ...) \
1076 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1079 static char * nl80211_ifadd(const char *ifname
)
1081 char path
[PATH_MAX
];
1082 static char nif
[IFNAMSIZ
] = { 0 };
1083 struct nl80211_msg_conveyor
*req
;
1086 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
1089 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
1091 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
1092 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
1094 nl80211_send(req
, NULL
, NULL
);
1096 snprintf(path
, sizeof(path
) - 1,
1097 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
1099 if ((sysfs
= fopen(path
, "w")) != NULL
)
1101 fwrite("0\n", 1, 2, sysfs
);
1114 static void nl80211_ifdel(const char *ifname
)
1116 struct nl80211_msg_conveyor
*req
;
1118 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1121 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1123 nl80211_send(req
, NULL
, NULL
);
1131 static void nl80211_hostapd_hup(const char *ifname
)
1135 char *phy
= nl80211_ifname2phy(ifname
);
1139 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1140 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1142 if (read(fd
, buf
, sizeof(buf
)) > 0)
1154 static int nl80211_probe(const char *ifname
)
1156 return !!nl80211_ifname2phy(ifname
);
1159 struct nl80211_ssid_bssid
{
1160 unsigned char *ssid
;
1161 unsigned char bssid
[7];
1164 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1166 struct nl80211_ssid_bssid
*sb
= arg
;
1167 struct nlattr
**tb
= nl80211_parse(msg
);
1169 if (tb
[NL80211_ATTR_MAC
]) {
1171 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1172 sizeof(sb
->bssid
) - 1);
1178 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1182 struct nl80211_ssid_bssid
*sb
= arg
;
1183 struct nlattr
**tb
= nl80211_parse(msg
);
1184 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1186 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1187 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1188 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1191 if (!tb
[NL80211_ATTR_BSS
] ||
1192 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1194 !bss
[NL80211_BSS_BSSID
] ||
1195 !bss
[NL80211_BSS_STATUS
] ||
1196 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1201 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1203 case NL80211_BSS_STATUS_ASSOCIATED
:
1204 case NL80211_BSS_STATUS_AUTHENTICATED
:
1205 case NL80211_BSS_STATUS_IBSS_JOINED
:
1209 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1210 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1212 while (ielen
>= 2 && ielen
>= ie
[1])
1216 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1227 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1236 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1239 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1241 /* try to find ssid from scan dump results */
1242 res
= nl80211_phy2ifname(ifname
);
1245 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1246 nl80211_get_ssid_bssid_cb
, &sb
);
1248 /* failed, try to find from hostapd info */
1249 if (sb
.ssid
[0] == 0)
1250 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1251 IWINFO_ESSID_MAX_SIZE
+ 1);
1253 /* failed, try to obtain Mesh ID */
1254 if (sb
.ssid
[0] == 0)
1255 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1256 buf
, IWINFO_ESSID_MAX_SIZE
+ 1);
1258 return (sb
.ssid
[0] == 0) ? -1 : 0;
1261 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1263 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1264 struct nl80211_ssid_bssid sb
= { };
1266 res
= nl80211_phy2ifname(ifname
);
1268 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1269 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1270 nl80211_get_macaddr_cb
, &sb
);
1272 /* failed, try to find bssid from scan dump results */
1273 if (sb
.bssid
[0] == 0)
1274 nl80211_request(res
? res
: ifname
,
1275 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1276 nl80211_get_ssid_bssid_cb
, &sb
);
1278 /* failed, try to find mac from hostapd info */
1279 if ((sb
.bssid
[0] == 0) &&
1280 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1283 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1284 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1285 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1286 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1287 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1288 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1293 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1294 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1295 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1304 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1307 struct nlattr
**attr
= nl80211_parse(msg
);
1308 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1310 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1311 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1312 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1315 if (attr
[NL80211_ATTR_BSS
] &&
1316 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1317 attr
[NL80211_ATTR_BSS
], bss_policy
))
1319 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1320 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1326 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1329 struct nlattr
**tb
= nl80211_parse(msg
);
1331 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1332 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1337 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1339 char *res
, channel
[4], hwmode
[3];
1341 /* try to find frequency from interface info */
1342 res
= nl80211_phy2ifname(ifname
);
1345 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1346 nl80211_get_frequency_info_cb
, buf
);
1348 /* failed, try to find frequency from hostapd info */
1350 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1351 "channel", channel
, sizeof(channel
)) == 2)
1353 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1356 /* failed, try to find frequency from scan results */
1359 res
= nl80211_phy2ifname(ifname
);
1361 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1362 nl80211_get_frequency_scan_cb
, buf
);
1365 return (*buf
== 0) ? -1 : 0;
1368 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1371 struct nlattr
**tb
= nl80211_parse(msg
);
1373 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1374 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1379 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1383 /* try to find frequency from interface info */
1384 res
= nl80211_phy2ifname(ifname
);
1387 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1388 nl80211_get_center_freq1_cb
, buf
);
1390 return (*buf
== 0) ? -1 : 0;
1393 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1396 struct nlattr
**tb
= nl80211_parse(msg
);
1398 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1399 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1404 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1408 /* try to find frequency from interface info */
1409 res
= nl80211_phy2ifname(ifname
);
1412 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1413 nl80211_get_center_freq2_cb
, buf
);
1415 return (*buf
== 0) ? -1 : 0;
1418 static int nl80211_get_channel(const char *ifname
, int *buf
)
1420 if (!nl80211_get_frequency(ifname
, buf
))
1422 *buf
= nl80211_freq2channel(*buf
);
1429 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1431 if (!nl80211_get_center_freq1(ifname
, buf
))
1433 *buf
= nl80211_freq2channel(*buf
);
1440 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1442 if (!nl80211_get_center_freq2(ifname
, buf
))
1444 *buf
= nl80211_freq2channel(*buf
);
1451 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1454 struct nlattr
**tb
= nl80211_parse(msg
);
1456 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1457 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1462 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1466 res
= nl80211_phy2ifname(ifname
);
1469 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1470 nl80211_get_txpower_cb
, buf
))
1477 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1481 struct nl80211_rssi_rate
*rr
= arg
;
1482 struct nlattr
**attr
= nl80211_parse(msg
);
1483 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1484 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1486 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1487 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1488 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1489 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1490 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1491 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1492 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1493 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1494 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1495 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1496 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1499 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1500 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1501 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1502 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1503 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1506 if (attr
[NL80211_ATTR_STA_INFO
])
1508 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1509 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1511 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1513 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1514 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1518 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1520 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1521 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1524 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1526 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1527 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1538 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1543 memset(r
, 0, sizeof(*r
));
1545 if ((d
= opendir("/sys/class/net")) != NULL
)
1547 while ((de
= readdir(d
)) != NULL
)
1549 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1550 (!de
->d_name
[strlen(ifname
)] ||
1551 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1553 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1554 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1562 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1564 struct nl80211_rssi_rate rr
;
1566 nl80211_fill_signal(ifname
, &rr
);
1568 if (rr
.rate_samples
)
1570 *buf
= (rr
.rate
* 100);
1577 static int nl80211_get_signal(const char *ifname
, int *buf
)
1579 struct nl80211_rssi_rate rr
;
1581 nl80211_fill_signal(ifname
, &rr
);
1583 if (rr
.rssi_samples
)
1592 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1594 int8_t *noise
= arg
;
1595 struct nlattr
**tb
= nl80211_parse(msg
);
1596 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1598 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1599 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1600 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1603 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1606 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1607 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1610 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1613 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1614 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1620 static int nl80211_get_noise(const char *ifname
, int *buf
)
1624 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1625 nl80211_get_noise_cb
, &noise
))
1636 static int nl80211_get_quality(const char *ifname
, int *buf
)
1640 if (!nl80211_get_signal(ifname
, &signal
))
1642 /* A positive signal level is usually just a quality
1643 * value, pass through as-is */
1649 /* The cfg80211 wext compat layer assumes a signal range
1650 * of -110 dBm to -40 dBm, the quality value is derived
1651 * by adding 110 to the signal level */
1656 else if (signal
> -40)
1659 *buf
= (signal
+ 110);
1668 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1670 /* The cfg80211 wext compat layer assumes a maximum
1677 static int nl80211_check_wepkey(const char *key
)
1681 switch (strlen(key
))
1685 return IWINFO_CIPHER_WEP40
;
1689 return IWINFO_CIPHER_WEP104
;
1700 } wpa_key_mgmt_strings
[] = {
1701 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1702 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1703 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1704 { "EAP-SHA384", 4, IWINFO_KMGMT_8021x
},
1705 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1706 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1707 { "NONE", 0, IWINFO_KMGMT_NONE
},
1708 { "None", 0, IWINFO_KMGMT_NONE
},
1709 { "PSK", 0, IWINFO_KMGMT_PSK
},
1710 { "EAP", 0, IWINFO_KMGMT_8021x
},
1711 { "SAE", 4, IWINFO_KMGMT_SAE
},
1712 { "OWE", 4, IWINFO_KMGMT_OWE
}
1715 static void parse_wpa_suites(const char *str
, int defversion
,
1716 uint8_t *versions
, uint8_t *suites
)
1720 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1726 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1728 m
= wpa_key_mgmt_strings
[i
].match
;
1731 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1733 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1734 version
= wpa_key_mgmt_strings
[i
].version
;
1736 version
= defversion
;
1738 *versions
|= version
;
1739 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1747 q
+= strcspn(q
, sep
);
1749 p
= q
+ strspn(q
, sep
);
1756 } wpa_cipher_strings
[] = {
1757 { "WEP-104", IWINFO_CIPHER_WEP104
},
1758 { "WEP-40", IWINFO_CIPHER_WEP40
},
1759 { "NONE", IWINFO_CIPHER_NONE
},
1760 { "TKIP", IWINFO_CIPHER_TKIP
},
1761 { "CCMP-256",IWINFO_CIPHER_CCMP256
},
1762 { "CCMP", IWINFO_CIPHER_CCMP
},
1763 { "GCMP-256",IWINFO_CIPHER_GCMP256
},
1764 { "GCMP", IWINFO_CIPHER_GCMP
}
1767 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1771 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1777 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1779 m
= wpa_cipher_strings
[i
].match
;
1782 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1784 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1792 q
+= strcspn(q
, sep
);
1794 p
= q
+ strspn(q
, sep
);
1798 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1802 uint8_t wpa_version
= 0;
1803 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1804 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1807 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1809 /* WPA supplicant */
1810 if (nl80211_wpactl_query(ifname
,
1811 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1812 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1813 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1814 "mode", mode
, sizeof(mode
)))
1817 if (!strcmp(wpa_key_mgmt
, "NONE"))
1819 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1820 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1822 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1824 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1825 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1828 c
->pair_ciphers
= 0;
1829 c
->group_ciphers
= 0;
1834 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1838 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1839 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1840 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1846 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1847 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1851 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1856 else if (!strncmp(p
, "WPA-", 4))
1862 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1864 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1871 else if (nl80211_hostapd_query(ifname
,
1872 "wpa", wpa
, sizeof(wpa
),
1873 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1874 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1875 "auth_algs", auth_algs
, sizeof(auth_algs
),
1876 "wep_key0", wep_key0
, sizeof(wep_key0
),
1877 "wep_key1", wep_key1
, sizeof(wep_key1
),
1878 "wep_key2", wep_key2
, sizeof(wep_key2
),
1879 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1883 if (wpa_key_mgmt
[0])
1885 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1887 if (!strncmp(p
, "WPA-", 4))
1890 if (!strncmp(p
, "FT-", 3))
1893 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1896 c
->enabled
= c
->wpa_version
? 1 : 0;
1899 if (wpa_pairwise
[0])
1900 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1904 switch (atoi(auth_algs
))
1907 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1911 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1915 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1916 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1920 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1921 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1922 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1923 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1925 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1928 c
->group_ciphers
= c
->pair_ciphers
;
1933 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1934 else if (!nl80211_get_mode(ifname
, &opmode
) &&
1935 (opmode
== IWINFO_OPMODE_ADHOC
||
1936 opmode
== IWINFO_OPMODE_MESHPOINT
))
1947 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1951 name
= nl80211_ifname2phy(ifname
);
1958 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1960 name
= nl80211_ifname2phy(name
);
1964 strcpy(buf
, ifname
);
1973 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1974 struct iwinfo_rate_entry
*re
)
1976 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1977 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1978 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1979 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1981 if (ri
[NL80211_RATE_INFO_HE_MCS
])
1984 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_MCS
]);
1986 if (ri
[NL80211_RATE_INFO_HE_NSS
])
1987 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_NSS
]);
1988 if (ri
[NL80211_RATE_INFO_HE_GI
])
1989 re
->he_gi
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_GI
]);
1990 if (ri
[NL80211_RATE_INFO_HE_DCM
])
1991 re
->he_dcm
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_DCM
]);
1993 else if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1996 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1998 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1999 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
2001 else if (ri
[NL80211_RATE_INFO_MCS
])
2004 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
2007 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
2009 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
2011 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
2013 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
2015 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
2016 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
2021 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
2022 re
->is_short_gi
= 1;
2024 re
->is_40mhz
= (re
->mhz
== 40);
2027 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
2029 struct nl80211_array_buf
*arr
= arg
;
2030 struct iwinfo_survey_entry
*e
= arr
->buf
;
2031 struct nlattr
**attr
= nl80211_parse(msg
);
2032 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
2035 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
2036 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
2037 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
2038 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
2039 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
2040 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
2041 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
2042 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
2045 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
2046 attr
[NL80211_ATTR_SURVEY_INFO
],
2051 /* advance to end of array */
2053 memset(e
, 0, sizeof(*e
));
2055 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
2056 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
2058 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
2059 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
2061 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
2062 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
2064 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
2065 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
2067 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
2068 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
2070 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
2071 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
2073 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
2074 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
2081 static void plink_state_to_str(char *dst
, unsigned state
)
2084 case NL80211_PLINK_LISTEN
:
2085 strcpy(dst
, "LISTEN");
2087 case NL80211_PLINK_OPN_SNT
:
2088 strcpy(dst
, "OPN_SNT");
2090 case NL80211_PLINK_OPN_RCVD
:
2091 strcpy(dst
, "OPN_RCVD");
2093 case NL80211_PLINK_CNF_RCVD
:
2094 strcpy(dst
, "CNF_RCVD");
2096 case NL80211_PLINK_ESTAB
:
2097 strcpy(dst
, "ESTAB");
2099 case NL80211_PLINK_HOLDING
:
2100 strcpy(dst
, "HOLDING");
2102 case NL80211_PLINK_BLOCKED
:
2103 strcpy(dst
, "BLOCKED");
2106 strcpy(dst
, "UNKNOWN");
2111 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
2113 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
2116 case NL80211_MESH_POWER_ACTIVE
:
2117 strcpy(dst
, "ACTIVE");
2119 case NL80211_MESH_POWER_LIGHT_SLEEP
:
2120 strcpy(dst
, "LIGHT SLEEP");
2122 case NL80211_MESH_POWER_DEEP_SLEEP
:
2123 strcpy(dst
, "DEEP SLEEP");
2126 strcpy(dst
, "UNKNOWN");
2131 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2133 struct nl80211_array_buf
*arr
= arg
;
2134 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2135 struct nlattr
**attr
= nl80211_parse(msg
);
2136 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2137 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2138 struct nl80211_sta_flag_update
*sta_flags
;
2140 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2141 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2142 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2143 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2144 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2145 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2146 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2147 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2148 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2149 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2150 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2151 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2152 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2153 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2154 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2155 [NL80211_STA_INFO_STA_FLAGS
] =
2156 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2157 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2159 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2160 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2161 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2162 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2163 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2164 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2167 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2168 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2169 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2170 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2171 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2174 /* advance to end of array */
2176 memset(e
, 0, sizeof(*e
));
2178 if (attr
[NL80211_ATTR_MAC
])
2179 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2181 if (attr
[NL80211_ATTR_STA_INFO
] &&
2182 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2183 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2185 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2186 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2188 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2189 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2191 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2192 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2194 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2195 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2197 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2198 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2200 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2201 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2203 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2204 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2205 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2206 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2208 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2209 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2210 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2211 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2213 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2214 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2216 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2217 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2219 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2220 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2222 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2223 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2225 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2226 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2228 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2229 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2231 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2232 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2235 if (sinfo
[NL80211_STA_INFO_LLID
])
2236 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2238 if (sinfo
[NL80211_STA_INFO_PLID
])
2239 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2241 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2242 plink_state_to_str(e
->plink_state
,
2243 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2245 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2246 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2247 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2248 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2249 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2250 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2253 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2255 sta_flags
= (struct nl80211_sta_flag_update
*)
2256 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2258 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2259 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2260 e
->is_authorized
= 1;
2262 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2263 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2264 e
->is_authenticated
= 1;
2266 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2267 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2268 e
->is_preamble_short
= 1;
2270 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2271 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2274 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2275 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2278 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2279 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2284 e
->noise
= 0; /* filled in by caller */
2290 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2292 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2295 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2296 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2298 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2305 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2310 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2311 struct iwinfo_assoclist_entry
*e
;
2313 if ((d
= opendir("/sys/class/net")) != NULL
)
2315 while ((de
= readdir(d
)) != NULL
)
2317 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2318 (!de
->d_name
[strlen(ifname
)] ||
2319 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2321 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2322 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2328 if (!nl80211_get_noise(ifname
, &noise
))
2329 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2332 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2339 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2342 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2344 struct nlattr
**attr
= nl80211_parse(msg
);
2345 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2346 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2347 struct nlattr
*band
, *freq
;
2349 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2350 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2351 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2352 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2353 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2354 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2355 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2358 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2361 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2363 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2364 nla_len(band
), NULL
);
2366 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2368 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2369 nla_data(freq
), nla_len(freq
), freq_policy
);
2371 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2372 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2374 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2375 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2377 *dbm_max
= (int)(0.01 * nla_get_u32(
2378 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2388 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2391 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2392 struct iwinfo_txpwrlist_entry entry
;
2394 if (nl80211_get_channel(ifname
, &ch_cur
))
2397 /* initialize the value pointer with channel for callback */
2400 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2401 nl80211_get_txpwrlist_cb
, &dbm_max
);
2405 for (dbm_cur
= 0, dbm_cnt
= 0;
2407 dbm_cur
++, dbm_cnt
++)
2409 entry
.dbm
= dbm_cur
;
2410 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2412 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2415 entry
.dbm
= dbm_max
;
2416 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2418 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2421 *len
= dbm_cnt
* sizeof(entry
);
2428 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2430 int wpa_version
= 0;
2431 char *p
, *q
, *proto
, *suites
;
2435 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2436 if (!strcmp(p
, "WEP")) {
2438 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2439 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2440 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2444 proto
= strtok(p
, "-");
2445 suites
= strtok(NULL
, "]");
2447 if (!proto
|| !suites
)
2450 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2452 else if (!strcmp(proto
, "WPA"))
2459 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2460 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2465 struct nl80211_scanlist
{
2466 struct iwinfo_scanlist_entry
*e
;
2471 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2472 struct iwinfo_scanlist_entry
*e
)
2474 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2475 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2476 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2479 while (ielen
>= 2 && ielen
>= ie
[1])
2484 case 114: /* Mesh ID */
2485 if (e
->ssid
[0] == 0) {
2486 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2487 memcpy(e
->ssid
, ie
+ 2, len
);
2493 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2494 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2497 case 221: /* Vendor */
2498 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2499 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2500 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2502 case 61: /* HT oeration */
2504 e
->ht_chan_info
.primary_chan
= ie
[2];
2505 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2506 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2509 case 192: /* VHT operation */
2511 e
->vht_chan_info
.chan_width
= ie
[2];
2512 e
->vht_chan_info
.center_chan_1
= ie
[3];
2513 e
->vht_chan_info
.center_chan_2
= ie
[4];
2523 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2528 struct nl80211_scanlist
*sl
= arg
;
2529 struct nlattr
**tb
= nl80211_parse(msg
);
2530 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2532 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2533 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2534 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2535 [NL80211_BSS_BSSID
] = { 0 },
2536 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2537 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2538 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2539 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2540 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2541 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2542 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2543 [NL80211_BSS_BEACON_IES
] = { 0 },
2546 if (!tb
[NL80211_ATTR_BSS
] ||
2547 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2549 !bss
[NL80211_BSS_BSSID
])
2554 if (bss
[NL80211_BSS_CAPABILITY
])
2555 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2559 memset(sl
->e
, 0, sizeof(*sl
->e
));
2560 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2563 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2564 else if (caps
& (1<<0))
2565 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2567 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2570 sl
->e
->crypto
.enabled
= 1;
2572 if (bss
[NL80211_BSS_FREQUENCY
])
2573 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2574 bss
[NL80211_BSS_FREQUENCY
]));
2576 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2577 nl80211_get_scanlist_ie(bss
, sl
->e
);
2579 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2582 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2584 rssi
= sl
->e
->signal
- 0x100;
2588 else if (rssi
> -40)
2591 sl
->e
->quality
= (rssi
+ 110);
2592 sl
->e
->quality_max
= 70;
2595 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2597 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2598 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2607 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2609 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2611 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2614 if (nl80211_wait("nl80211", "scan",
2615 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2618 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2619 nl80211_get_scanlist_cb
, &sl
))
2622 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2630 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2633 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2634 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2640 if (len
+ 1 >= outlen
)
2650 out
[len
++] = '\n'; in
++;
2654 out
[len
++] = '\r'; in
++;
2658 out
[len
++] = '\t'; in
++;
2662 out
[len
++] = '\033'; in
++;
2666 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2667 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2689 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2691 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2692 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2693 struct sockaddr_un local
= { 0 };
2694 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2696 sock
= nl80211_wpactl_connect(ifname
, &local
);
2701 send(sock
, "ATTACH", 6, 0);
2702 send(sock
, "SCAN", 4, 0);
2705 * wait for scan results:
2706 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2707 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2708 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2709 * time to process the results, so try 72 + 2 + 1 times.
2711 for (tries
= 0; tries
< 75; tries
++)
2713 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2716 /* got an event notification */
2717 if (reply
[0] == '<')
2719 /* scan results are ready */
2720 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2722 /* send "SCAN_RESULTS" command */
2723 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2727 /* is another unrelated event, retry */
2731 /* scanning already in progress, keep awaiting results */
2732 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2737 /* another failure, abort */
2738 else if (!strncmp(reply
, "FAIL-", 5))
2744 /* receive and parse scan results if the wait above didn't time out */
2745 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2747 /* received an event notification, receive again */
2748 if (reply
[0] == '<')
2751 nl80211_get_quality_max(ifname
, &qmax
);
2753 for (line
= strtok_r(reply
, "\n", &pos
);
2755 line
= strtok_r(NULL
, "\n", &pos
))
2757 /* skip header line */
2764 bssid
= strtok(line
, "\t");
2765 freq
= strtok(NULL
, "\t");
2766 signal
= strtok(NULL
, "\t");
2767 flags
= strtok(NULL
, "\t");
2768 ssid
= strtok(NULL
, "\n");
2770 if (!bssid
|| !freq
|| !signal
|| !flags
)
2774 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2775 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2776 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2777 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2778 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2779 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2783 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2788 if (strstr(flags
, "[MESH]"))
2789 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2790 else if (strstr(flags
, "[IBSS]"))
2791 e
->mode
= IWINFO_OPMODE_ADHOC
;
2793 e
->mode
= IWINFO_OPMODE_MASTER
;
2796 e
->channel
= nl80211_freq2channel(atoi(freq
));
2799 rssi
= atoi(signal
);
2805 /* The cfg80211 wext compat layer assumes a signal range
2806 * of -110 dBm to -40 dBm, the quality value is derived
2807 * by adding 110 to the signal level */
2810 else if (rssi
> -40)
2813 e
->quality
= (rssi
+ 110);
2821 e
->quality_max
= qmax
;
2824 nl80211_get_scancrypto(flags
, &e
->crypto
);
2830 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2835 unlink(local
.sun_path
);
2837 return (count
>= 0) ? 0 : -1;
2840 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2847 /* Got a radioX pseudo interface, find some interface on it or create one */
2848 if (!strncmp(ifname
, "radio", 5))
2850 /* Reuse existing interface */
2851 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2853 return nl80211_get_scanlist(res
, buf
, len
);
2856 /* Need to spawn a temporary iface for scanning */
2857 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2859 rv
= nl80211_get_scanlist(res
, buf
, len
);
2865 /* WPA supplicant */
2866 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2871 /* station / ad-hoc / monitor scan */
2872 else if (!nl80211_get_mode(ifname
, &mode
) &&
2873 (mode
== IWINFO_OPMODE_ADHOC
||
2874 mode
== IWINFO_OPMODE_MASTER
||
2875 mode
== IWINFO_OPMODE_CLIENT
||
2876 mode
== IWINFO_OPMODE_MONITOR
) &&
2877 iwinfo_ifup(ifname
))
2879 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2885 /* Got a temp interface, don't create yet another one */
2886 if (!strncmp(ifname
, "tmp.", 4))
2888 if (!iwinfo_ifup(ifname
))
2891 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2892 iwinfo_ifdown(ifname
);
2896 /* Spawn a new scan interface */
2899 if (!(res
= nl80211_ifadd(ifname
)))
2904 /* if we can take the new interface up, the driver supports an
2905 * additional interface and there's no need to tear down the ap */
2906 if (iwinfo_ifup(res
))
2908 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2912 /* driver cannot create secondary interface, take down ap
2914 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2916 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2918 iwinfo_ifup(ifname
);
2919 nl80211_hostapd_hup(ifname
);
2932 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2934 int bands_remain
, freqs_remain
;
2936 struct nl80211_array_buf
*arr
= arg
;
2937 struct iwinfo_freqlist_entry
*e
;
2939 struct nlattr
**attr
= nl80211_parse(msg
);
2940 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2941 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2942 struct nlattr
*band
, *freq
;
2947 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2948 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2950 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2951 nla_data(band
), nla_len(band
), NULL
);
2953 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2954 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2956 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2957 nla_data(freq
), nla_len(freq
), NULL
);
2959 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2960 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2963 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2964 e
->channel
= nl80211_freq2channel(e
->mhz
);
2967 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2968 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2971 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2972 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2973 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2974 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2975 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2976 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2977 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2978 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2979 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2980 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2981 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2982 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2994 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2996 struct nl80211_msg_conveyor
*cv
;
2997 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2998 uint32_t features
= nl80211_get_protocol_features(ifname
);
3001 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3002 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3006 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3007 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
3010 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
3020 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
3023 struct nlattr
**attr
= nl80211_parse(msg
);
3025 if (attr
[NL80211_ATTR_REG_ALPHA2
])
3026 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
3033 static int nl80211_get_country(const char *ifname
, char *buf
)
3035 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
3036 nl80211_get_country_cb
, buf
))
3042 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
3045 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
3046 const struct iwinfo_iso3166_label
*l
;
3048 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
3050 e
->iso3166
= l
->iso3166
;
3051 e
->ccode
[0] = (l
->iso3166
/ 256);
3052 e
->ccode
[1] = (l
->iso3166
% 256);
3056 *len
= (count
* sizeof(struct iwinfo_country_entry
));
3061 struct nl80211_modes
3071 uint16_t he_phy_cap
[6];
3074 static void nl80211_eval_modelist(struct nl80211_modes
*m
)
3076 /* Treat any nonzero capability as 11n */
3079 m
->hw
|= IWINFO_80211_N
;
3080 m
->ht
|= IWINFO_HTMODE_HT20
;
3082 if (m
->nl_ht
& (1 << 1))
3083 m
->ht
|= IWINFO_HTMODE_HT40
;
3086 if (m
->he_phy_cap
[0] != 0) {
3087 m
->hw
|= IWINFO_80211_AX
;
3088 m
->ht
|= IWINFO_HTMODE_HE20
;
3090 if (m
->he_phy_cap
[0] & BIT(9))
3091 m
->ht
|= IWINFO_HTMODE_HE40
;
3092 if (m
->he_phy_cap
[0] & BIT(10))
3093 m
->ht
|= IWINFO_HTMODE_HE40
| IWINFO_HTMODE_HE80
;
3094 if (m
->he_phy_cap
[0] & BIT(11))
3095 m
->ht
|= IWINFO_HTMODE_HE160
;
3096 if (m
->he_phy_cap
[0] & BIT(12))
3097 m
->ht
|= IWINFO_HTMODE_HE160
| IWINFO_HTMODE_HE80_80
;
3100 if (m
->bands
& IWINFO_BAND_24
)
3102 m
->hw
|= IWINFO_80211_B
;
3103 m
->hw
|= IWINFO_80211_G
;
3106 if (m
->bands
& IWINFO_BAND_5
)
3108 /* Treat any nonzero capability as 11ac */
3111 m
->hw
|= IWINFO_80211_AC
;
3112 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3114 switch ((m
->nl_vht
>> 2) & 3)
3117 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3121 m
->ht
|= IWINFO_HTMODE_VHT160
;
3126 m
->hw
|= IWINFO_80211_A
;
3130 if (m
->bands
& IWINFO_BAND_60
)
3132 m
->hw
|= IWINFO_80211_AD
;
3137 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
3139 struct nl80211_modes
*m
= arg
;
3140 int bands_remain
, freqs_remain
;
3141 struct nlattr
**attr
= nl80211_parse(msg
);
3142 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3143 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
3144 struct nlattr
*band
, *freq
;
3147 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
3149 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3151 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3152 nla_data(band
), nla_len(band
), NULL
);
3154 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
3155 m
->nl_ht
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
3157 if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
3158 m
->nl_vht
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
3160 if (bands
[NL80211_BAND_ATTR_IFTYPE_DATA
]) {
3161 struct nlattr
*tb
[NL80211_BAND_IFTYPE_ATTR_MAX
+ 1];
3162 struct nlattr
*nl_iftype
;
3166 nla_for_each_nested(nl_iftype
, bands
[NL80211_BAND_ATTR_IFTYPE_DATA
], rem_band
) {
3167 nla_parse(tb
, NL80211_BAND_IFTYPE_ATTR_MAX
,
3168 nla_data(nl_iftype
), nla_len(nl_iftype
), NULL
);
3169 if (tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]) {
3170 len
= nla_len(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]);
3172 if (len
> sizeof(m
->he_phy_cap
) - 1)
3173 len
= sizeof(m
->he_phy_cap
) - 1;
3174 memcpy(&((__u8
*)m
->he_phy_cap
)[1],
3175 nla_data(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]),
3181 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
3182 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
3185 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
3186 nla_data(freq
), nla_len(freq
), NULL
);
3188 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
3191 freq_mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
3193 if (freq_mhz
> 2400 && freq_mhz
< 2485)
3195 m
->bands
|= IWINFO_BAND_24
;
3197 else if (freq_mhz
> 5000 && freq_mhz
< 5850)
3199 m
->bands
|= IWINFO_BAND_5
;
3201 else if (freq_mhz
> 6000 && freq_mhz
< 7120)
3203 m
->bands
|= IWINFO_BAND_6
;
3205 else if (freq_mhz
>= 56160)
3207 m
->bands
|= IWINFO_BAND_60
;
3219 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3221 struct nl80211_msg_conveyor
*cv
;
3222 struct nl80211_modes m
= {};
3223 uint32_t features
= nl80211_get_protocol_features(ifname
);
3226 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3227 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3231 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3232 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3233 goto nla_put_failure
;
3235 nl80211_eval_modelist(&m
);
3252 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3254 struct nlattr
**tb
= nl80211_parse(msg
);
3256 struct chan_info
*chn
= arg
;
3258 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3259 chn
->width
= nla_get_u32(cur
);
3261 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3262 chn
->mode
= nla_get_u32(cur
);
3267 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3269 struct chan_info chn
= { .width
= 0, .mode
= 0 };
3273 res
= nl80211_phy2ifname(ifname
);
3276 err
= nl80211_request(res
? res
: ifname
,
3277 NL80211_CMD_GET_INTERFACE
, 0,
3278 nl80211_get_htmode_cb
, &chn
);
3282 switch (chn
.width
) {
3283 case NL80211_CHAN_WIDTH_20
:
3285 *buf
= IWINFO_HTMODE_VHT20
;
3287 *buf
= IWINFO_HTMODE_HT20
;
3289 case NL80211_CHAN_WIDTH_40
:
3291 *buf
= IWINFO_HTMODE_VHT40
;
3293 *buf
= IWINFO_HTMODE_HT40
;
3295 case NL80211_CHAN_WIDTH_80
:
3296 *buf
= IWINFO_HTMODE_VHT80
;
3298 case NL80211_CHAN_WIDTH_80P80
:
3299 *buf
= IWINFO_HTMODE_VHT80_80
;
3301 case NL80211_CHAN_WIDTH_160
:
3302 *buf
= IWINFO_HTMODE_VHT160
;
3304 case NL80211_CHAN_WIDTH_5
:
3305 case NL80211_CHAN_WIDTH_10
:
3306 case NL80211_CHAN_WIDTH_20_NOHT
:
3307 *buf
= IWINFO_HTMODE_NOHT
;
3316 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3318 struct nl80211_msg_conveyor
*cv
;
3319 struct nl80211_modes m
= {};
3320 uint32_t features
= nl80211_get_protocol_features(ifname
);
3323 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3324 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3328 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3329 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3330 goto nla_put_failure
;
3332 nl80211_eval_modelist(&m
);
3345 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3347 struct nlattr
**attr
= nl80211_parse(msg
);
3348 struct nlattr
*comb
;
3350 int comb_rem
, limit_rem
, mode_rem
;
3353 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3356 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3358 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3359 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3360 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3362 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3363 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3364 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3365 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3367 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3368 struct nlattr
*limit
;
3370 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3372 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3375 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3377 struct nlattr
*mode
;
3379 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3381 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3382 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3385 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3388 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3389 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3398 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3400 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3401 nl80211_get_ifcomb_cb
, buf
))
3407 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3409 char *phy
, compat
[64], path
[PATH_MAX
];
3411 /* Try to determine the phy name from the given interface */
3412 phy
= nl80211_ifname2phy(ifname
);
3414 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3415 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3417 if (nl80211_readstr(path
, compat
, sizeof(compat
)) <= 0)
3420 if (!strcmp(compat
, "qca,ar9130-wmac")) {
3421 id
->vendor_id
= 0x168c;
3422 id
->device_id
= 0x0029;
3423 id
->subsystem_vendor_id
= 0x168c;
3424 id
->subsystem_device_id
= 0x9130;
3425 } else if (!strcmp(compat
, "qca,ar9330-wmac")) {
3426 id
->vendor_id
= 0x168c;
3427 id
->device_id
= 0x0030;
3428 id
->subsystem_vendor_id
= 0x168c;
3429 id
->subsystem_device_id
= 0x9330;
3430 } else if (!strcmp(compat
, "qca,ar9340-wmac")) {
3431 id
->vendor_id
= 0x168c;
3432 id
->device_id
= 0x0030;
3433 id
->subsystem_vendor_id
= 0x168c;
3434 id
->subsystem_device_id
= 0x9340;
3435 } else if (!strcmp(compat
, "qca,qca9530-wmac")) {
3436 id
->vendor_id
= 0x168c;
3437 id
->device_id
= 0x0033;
3438 id
->subsystem_vendor_id
= 0x168c;
3439 id
->subsystem_device_id
= 0x9530;
3440 } else if (!strcmp(compat
, "qca,qca9550-wmac")) {
3441 id
->vendor_id
= 0x168c;
3442 id
->device_id
= 0x0033;
3443 id
->subsystem_vendor_id
= 0x168c;
3444 id
->subsystem_device_id
= 0x9550;
3445 } else if (!strcmp(compat
, "qca,qca9560-wmac")) {
3446 id
->vendor_id
= 0x168c;
3447 id
->device_id
= 0x0033;
3448 id
->subsystem_vendor_id
= 0x168c;
3449 id
->subsystem_device_id
= 0x9560;
3450 } else if (!strcmp(compat
, "qcom,ipq4019-wifi")) {
3451 id
->vendor_id
= 0x168c;
3452 id
->device_id
= 0x003c;
3453 id
->subsystem_vendor_id
= 0x168c;
3454 id
->subsystem_device_id
= 0x4019;
3455 } else if (!strcmp(compat
, "mediatek,mt7622-wmac")) {
3456 id
->vendor_id
= 0x14c3;
3457 id
->device_id
= 0x7622;
3458 id
->subsystem_vendor_id
= 0x14c3;
3459 id
->subsystem_device_id
= 0x7622;
3460 } else if (!strcmp(compat
, "mediatek,mt7986-wmac")) {
3461 id
->vendor_id
= 0x14c3;
3462 id
->device_id
= 0x7986;
3463 id
->subsystem_vendor_id
= 0x14c3;
3464 id
->subsystem_device_id
= 0x7986;
3467 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
3471 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3473 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3474 char *phy
, num
[8], path
[PATH_MAX
];
3477 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3478 { "vendor", &id
->vendor_id
},
3479 { "device", &id
->device_id
},
3480 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3481 { "subsystem_device", &id
->subsystem_device_id
},
3482 { "../idVendor", &id
->subsystem_vendor_id
},
3483 { "../idProduct", &id
->subsystem_device_id
}
3486 memset(id
, 0, sizeof(*id
));
3488 /* Try to determine the phy name from the given interface */
3489 phy
= nl80211_ifname2phy(ifname
);
3491 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3493 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3494 phy
? "ieee80211" : "net",
3495 phy
? phy
: ifname
, lookup
[i
].path
);
3497 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3498 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3501 /* Failed to obtain hardware IDs, try FDT */
3502 if (id
->vendor_id
== 0 && id
->device_id
== 0 &&
3503 id
->subsystem_vendor_id
== 0 && id
->subsystem_device_id
== 0)
3504 if (!nl80211_hardware_id_from_fdt(id
, ifname
))
3507 /* Failed to obtain hardware IDs, search board config */
3508 if (id
->vendor_id
== 0 && id
->device_id
== 0 &&
3509 id
->subsystem_vendor_id
== 0 && id
->subsystem_device_id
== 0)
3510 return iwinfo_hardware_id_from_mtd(id
);
3515 static const struct iwinfo_hardware_entry
*
3516 nl80211_get_hardware_entry(const char *ifname
)
3518 struct iwinfo_hardware_id id
;
3520 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3523 return iwinfo_hardware(&id
);
3526 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3528 const struct iwinfo_hardware_entry
*hw
;
3530 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3531 sprintf(buf
, "Generic MAC80211");
3533 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3538 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3540 const struct iwinfo_hardware_entry
*hw
;
3542 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3545 *buf
= hw
->txpower_offset
;
3549 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3551 const struct iwinfo_hardware_entry
*hw
;
3553 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3556 *buf
= hw
->frequency_offset
;
3560 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3565 if (!strncmp(section
, "path=", 5))
3566 idx
= nl80211_phy_idx_from_path(section
+ 5);
3567 else if (!strncmp(section
, "macaddr=", 8))
3568 idx
= nl80211_phy_idx_from_macaddr(section
+ 8);
3570 idx
= nl80211_phy_idx_from_uci(section
);
3575 name
= nl80211_phyidx2name(idx
);
3583 static int nl80211_phy_path(const char *phyname
, const char **path
)
3585 if (strchr(phyname
, '/'))
3588 *path
= nl80211_phy_path_str(phyname
);
3595 const struct iwinfo_ops nl80211_ops
= {
3597 .probe
= nl80211_probe
,
3598 .channel
= nl80211_get_channel
,
3599 .center_chan1
= nl80211_get_center_chan1
,
3600 .center_chan2
= nl80211_get_center_chan2
,
3601 .frequency
= nl80211_get_frequency
,
3602 .frequency_offset
= nl80211_get_frequency_offset
,
3603 .txpower
= nl80211_get_txpower
,
3604 .txpower_offset
= nl80211_get_txpower_offset
,
3605 .bitrate
= nl80211_get_bitrate
,
3606 .signal
= nl80211_get_signal
,
3607 .noise
= nl80211_get_noise
,
3608 .quality
= nl80211_get_quality
,
3609 .quality_max
= nl80211_get_quality_max
,
3610 .mbssid_support
= nl80211_get_mbssid_support
,
3611 .hwmodelist
= nl80211_get_hwmodelist
,
3612 .htmodelist
= nl80211_get_htmodelist
,
3613 .htmode
= nl80211_get_htmode
,
3614 .mode
= nl80211_get_mode
,
3615 .ssid
= nl80211_get_ssid
,
3616 .bssid
= nl80211_get_bssid
,
3617 .country
= nl80211_get_country
,
3618 .hardware_id
= nl80211_get_hardware_id
,
3619 .hardware_name
= nl80211_get_hardware_name
,
3620 .encryption
= nl80211_get_encryption
,
3621 .phyname
= nl80211_get_phyname
,
3622 .assoclist
= nl80211_get_assoclist
,
3623 .txpwrlist
= nl80211_get_txpwrlist
,
3624 .scanlist
= nl80211_get_scanlist
,
3625 .freqlist
= nl80211_get_freqlist
,
3626 .countrylist
= nl80211_get_countrylist
,
3627 .survey
= nl80211_get_survey
,
3628 .lookup_phy
= nl80211_lookup_phyname
,
3629 .phy_path
= nl80211_phy_path
,
3630 .close
= nl80211_close