2 * iwinfo - Wireless Information Library - NL80211 Backend
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
18 * The signal handling code is derived from the official madwifi tools,
19 * wlanconfig.c in particular. The encryption property handling was
20 * inspired by the hostapd madwifi driver.
22 * Parts of this code are derived from the Linux iw utility.
31 #include "iwinfo_nl80211.h"
33 #define min(x, y) ((x) < (y)) ? (x) : (y)
35 #define BIT(x) (1ULL<<(x))
37 static struct nl80211_state
*nls
= NULL
;
39 static void nl80211_close(void)
44 genl_family_put(nls
->nlctrl
);
47 genl_family_put(nls
->nl80211
);
50 nl_socket_free(nls
->nl_sock
);
53 nl_cache_free(nls
->nl_cache
);
60 static int nl80211_init(void)
66 nls
= malloc(sizeof(struct nl80211_state
));
72 memset(nls
, 0, sizeof(*nls
));
74 nls
->nl_sock
= nl_socket_alloc();
80 if (genl_connect(nls
->nl_sock
)) {
85 fd
= nl_socket_get_fd(nls
->nl_sock
);
86 if (fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
) < 0) {
91 if (genl_ctrl_alloc_cache(nls
->nl_sock
, &nls
->nl_cache
)) {
96 nls
->nl80211
= genl_ctrl_search_by_name(nls
->nl_cache
, "nl80211");
102 nls
->nlctrl
= genl_ctrl_search_by_name(nls
->nl_cache
, "nlctrl");
117 static int nl80211_readint(const char *path
)
123 if ((fd
= open(path
, O_RDONLY
)) > -1)
125 if (read(fd
, buffer
, sizeof(buffer
)) > 0)
134 static int nl80211_readstr(const char *path
, char *buffer
, int length
)
139 if ((fd
= open(path
, O_RDONLY
)) > -1)
141 if ((rv
= read(fd
, buffer
, length
- 1)) > 0)
143 if (buffer
[rv
- 1] == '\n')
156 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
157 struct nlmsgerr
*err
, void *arg
)
164 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
171 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
178 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
183 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
198 static struct nl80211_msg_conveyor
* nl80211_new(struct genl_family
*family
,
201 static struct nl80211_msg_conveyor cv
;
203 struct nl_msg
*req
= NULL
;
204 struct nl_cb
*cb
= NULL
;
210 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
214 genlmsg_put(req
, 0, 0, genl_family_get_id(family
), 0, flags
, cmd
, 0);
228 static struct nl80211_msg_conveyor
* nl80211_ctl(int cmd
, int flags
)
230 if (nl80211_init() < 0)
233 return nl80211_new(nls
->nlctrl
, cmd
, flags
);
236 static const char *nl80211_phy_path_str(const char *phyname
)
238 static char path
[PATH_MAX
];
239 const char *prefix
= "/sys/devices/";
240 int prefix_len
= strlen(prefix
);
243 char buf
[128], *link
;
248 if (strncmp(phyname
, "phy", 3) != 0)
251 phy_id
= atoi(phyname
+ 3);
252 buf_len
= snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/device", phyname
);
253 link
= realpath(buf
, path
);
257 if (strncmp(link
, prefix
, prefix_len
) != 0)
262 prefix
= "platform/";
263 prefix_len
= strlen(prefix
);
264 if (!strncmp(link
, prefix
, prefix_len
) && strstr(link
, "/pci"))
267 snprintf(buf
+ buf_len
, sizeof(buf
) - buf_len
, "/ieee80211");
272 while ((e
= readdir(d
)) != NULL
) {
275 if (strncmp(e
->d_name
, "phy", 3) != 0)
278 cur_id
= atoi(e
->d_name
+ 3);
279 if (cur_id
>= phy_id
)
290 offset
= link
- path
+ strlen(link
);
291 snprintf(path
+ offset
, sizeof(path
) - offset
, "+%d", seq
);
296 static int nl80211_phy_idx_from_uci_path(struct uci_section
*s
)
300 const char *path
, *cur_path
;
304 path
= uci_lookup_option_string(uci_ctx
, s
, "path");
308 d
= opendir("/sys/class/ieee80211");
312 while ((e
= readdir(d
)) != NULL
) {
313 cur_path
= nl80211_phy_path_str(e
->d_name
);
317 if (strcmp(cur_path
, path
) != 0)
320 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
321 idx
= nl80211_readint(buf
);
332 static int nl80211_phy_idx_from_uci_macaddr(struct uci_section
*s
)
339 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
343 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
344 if (glob(buf
, 0, NULL
, &gl
))
347 for (i
= 0; i
< gl
.gl_pathc
; i
++)
349 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
350 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
353 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
356 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
357 if ((idx
= nl80211_readint(buf
)) > -1)
366 static int nl80211_phy_idx_from_uci_phy(struct uci_section
*s
)
371 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
375 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
376 return nl80211_readint(buf
);
379 static int nl80211_phy_idx_from_uci(const char *name
)
381 struct uci_section
*s
;
384 s
= iwinfo_uci_get_radio(name
, "mac80211");
388 idx
= nl80211_phy_idx_from_uci_path(s
);
391 idx
= nl80211_phy_idx_from_uci_macaddr(s
);
394 idx
= nl80211_phy_idx_from_uci_phy(s
);
401 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
404 int ifidx
= -1, phyidx
= -1;
405 struct nl80211_msg_conveyor
*cv
;
410 if (nl80211_init() < 0)
413 if (!strncmp(ifname
, "phy", 3))
414 phyidx
= atoi(&ifname
[3]);
415 else if (!strncmp(ifname
, "radio", 5))
416 phyidx
= nl80211_phy_idx_from_uci(ifname
);
418 if (!strncmp(ifname
, "mon.", 4))
419 ifidx
= if_nametoindex(&ifname
[4]);
421 ifidx
= if_nametoindex(ifname
);
423 /* Valid ifidx must be greater than 0 */
424 if ((ifidx
<= 0) && (phyidx
< 0))
427 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
432 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
433 else if (phyidx
> -1)
434 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
443 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
444 int (*cb_func
)(struct nl_msg
*, void *),
447 static struct nl80211_msg_conveyor rcv
;
451 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
453 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
455 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
462 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
463 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
464 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
467 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
474 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
475 int (*cb_func
)(struct nl_msg
*, void *),
478 struct nl80211_msg_conveyor
*cv
;
480 cv
= nl80211_msg(ifname
, cmd
, flags
);
485 return nl80211_send(cv
, cb_func
, cb_arg
);
488 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
490 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
491 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
493 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
494 genlmsg_attrlen(gnlh
, 0), NULL
);
499 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
501 uint32_t *features
= arg
;
502 struct nlattr
**attr
= nl80211_parse(msg
);
504 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
505 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
510 static int nl80211_get_protocol_features(const char *ifname
)
512 struct nl80211_msg_conveyor
*req
;
513 uint32_t features
= 0;
515 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
517 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
524 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
526 struct nl80211_group_conveyor
*cv
= arg
;
528 struct nlattr
**attr
= nl80211_parse(msg
);
529 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
533 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
536 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
538 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
539 nla_data(mgrp
), nla_len(mgrp
), NULL
);
541 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
542 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
543 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
544 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
546 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
554 static int nl80211_subscribe(const char *family
, const char *group
)
556 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
557 struct nl80211_msg_conveyor
*req
;
560 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
563 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
564 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
569 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
579 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
581 struct nl80211_event_conveyor
*cv
= arg
;
582 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
584 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
585 cv
->recv
= gnlh
->cmd
;
590 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
595 static int __nl80211_wait(const char *family
, const char *group
, ...)
597 struct nl80211_event_conveyor cv
= { };
603 if (nl80211_subscribe(family
, group
))
606 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
611 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
612 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
613 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
617 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
618 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
622 while (!cv
.recv
&& !err
)
623 nl_recvmsgs(nls
->nl_sock
, cb
);
630 #define nl80211_wait(family, group, ...) \
631 __nl80211_wait(family, group, __VA_ARGS__, 0)
634 static int nl80211_freq2channel(int freq
)
638 else if (freq
< 2484)
639 return (freq
- 2407) / 5;
640 else if (freq
>= 4910 && freq
<= 4980)
641 return (freq
- 4000) / 5;
642 else if(freq
>= 56160 + 2160 * 1 && freq
<= 56160 + 2160 * 6)
643 return (freq
- 56160) / 2160;
645 return (freq
- 5000) / 5;
648 static int nl80211_channel2freq(int channel
, const char *band
)
650 if (!band
|| band
[0] != 'a')
654 else if (channel
< 14)
655 return (channel
* 5) + 2407;
657 else if ( strcmp(band
, "ad") == 0)
659 return 56160 + 2160 * channel
;
663 if (channel
>= 182 && channel
<= 196)
664 return (channel
* 5) + 4000;
666 return (channel
* 5) + 5000;
672 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
675 struct nlattr
**attr
= nl80211_parse(msg
);
677 if (attr
[NL80211_ATTR_WIPHY_NAME
])
678 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
679 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
686 static char * nl80211_ifname2phy(const char *ifname
)
688 static char phy
[32] = { 0 };
690 memset(phy
, 0, sizeof(phy
));
692 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
693 nl80211_ifname2phy_cb
, phy
);
695 return phy
[0] ? phy
: NULL
;
698 static char * nl80211_phy2ifname(const char *ifname
)
700 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
702 static char nif
[IFNAMSIZ
] = { 0 };
707 /* Only accept phy name of the form phy%d or radio%d */
710 else if (!strncmp(ifname
, "phy", 3))
711 phyidx
= atoi(&ifname
[3]);
712 else if (!strncmp(ifname
, "radio", 5))
713 phyidx
= nl80211_phy_idx_from_uci(ifname
);
717 memset(nif
, 0, sizeof(nif
));
721 if ((d
= opendir("/sys/class/net")) != NULL
)
723 while ((e
= readdir(d
)) != NULL
)
725 snprintf(buffer
, sizeof(buffer
),
726 "/sys/class/net/%s/phy80211/index", e
->d_name
);
728 if (nl80211_readint(buffer
) == phyidx
)
730 snprintf(buffer
, sizeof(buffer
),
731 "/sys/class/net/%s/ifindex", e
->d_name
);
733 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
734 ((ifidx
< 0) || (cifidx
< ifidx
)))
737 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
746 return nif
[0] ? nif
: NULL
;
749 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
752 struct nlattr
**tb
= nl80211_parse(msg
);
753 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
754 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
755 IWINFO_OPMODE_ADHOC
, /* IBSS */
756 IWINFO_OPMODE_CLIENT
, /* managed */
757 IWINFO_OPMODE_MASTER
, /* AP */
758 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
759 IWINFO_OPMODE_WDS
, /* WDS */
760 IWINFO_OPMODE_MONITOR
, /* monitor */
761 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
762 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
763 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
766 if (tb
[NL80211_ATTR_IFTYPE
])
767 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
773 static int nl80211_get_mode(const char *ifname
, int *buf
)
777 *buf
= IWINFO_OPMODE_UNKNOWN
;
779 res
= nl80211_phy2ifname(ifname
);
781 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
782 nl80211_get_mode_cb
, buf
);
784 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
787 static int __nl80211_hostapd_query(const char *ifname
, ...)
790 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
791 int len
, mode
, found
= 0, match
= 1;
794 if (nl80211_get_mode(ifname
, &mode
))
797 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
800 phy
= nl80211_ifname2phy(ifname
);
805 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
806 fp
= fopen(buf
, "r");
811 va_start(ap
, ifname
);
813 /* clear all destination buffers */
816 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
818 dest
= va_arg(ap_cur
, char *);
819 len
= va_arg(ap_cur
, int);
821 memset(dest
, 0, len
);
826 /* iterate applicable lines and copy found values into dest buffers */
827 while (fgets(buf
, sizeof(buf
), fp
))
829 key
= strtok(buf
, " =\t\n");
830 val
= strtok(NULL
, "\n");
832 if (!key
|| !val
|| !*key
|| *key
== '#')
835 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
836 match
= !strcmp(ifname
, val
);
843 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
845 dest
= va_arg(ap_cur
, char *);
846 len
= va_arg(ap_cur
, int);
848 if (!strcmp(search
, key
))
850 strncpy(dest
, val
, len
- 1);
866 #define nl80211_hostapd_query(ifname, ...) \
867 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
870 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
873 struct timeval tv
= { 0, 256000 };
878 memset(buf
, 0, blen
);
880 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
883 if (!FD_ISSET(sock
, &rfds
))
886 return recv(sock
, buf
, blen
- 1, 0);
889 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
891 struct sockaddr_un remote
= { 0 };
892 size_t remote_length
, local_length
;
894 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
898 remote
.sun_family
= AF_UNIX
;
899 remote_length
= sizeof(remote
.sun_family
) +
900 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
903 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
909 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
911 remote_length
= sizeof(remote
.sun_family
) +
912 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
914 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
921 local
->sun_family
= AF_UNIX
;
922 local_length
= sizeof(local
->sun_family
) +
923 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
925 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
934 static int __nl80211_wpactl_query(const char *ifname
, ...)
937 struct sockaddr_un local
= { 0 };
938 int len
, mode
, found
= 0, sock
= -1;
939 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
941 if (nl80211_get_mode(ifname
, &mode
))
944 if (mode
!= IWINFO_OPMODE_CLIENT
&&
945 mode
!= IWINFO_OPMODE_ADHOC
&&
946 mode
!= IWINFO_OPMODE_MESHPOINT
)
949 sock
= nl80211_wpactl_connect(ifname
, &local
);
954 va_start(ap
, ifname
);
956 /* clear all destination buffers */
959 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
961 dest
= va_arg(ap_cur
, char *);
962 len
= va_arg(ap_cur
, int);
964 memset(dest
, 0, len
);
969 send(sock
, "STATUS", 6, 0);
973 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
979 for (line
= strtok_r(buf
, "\n", &pos
);
981 line
= strtok_r(NULL
, "\n", &pos
))
983 key
= strtok(line
, "=");
984 val
= strtok(NULL
, "\n");
991 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
993 dest
= va_arg(ap_cur
, char *);
994 len
= va_arg(ap_cur
, int);
996 if (!strcmp(search
, key
))
998 strncpy(dest
, val
, len
- 1);
1013 unlink(local
.sun_path
);
1018 #define nl80211_wpactl_query(ifname, ...) \
1019 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1022 static char * nl80211_ifadd(const char *ifname
)
1024 char path
[PATH_MAX
];
1025 static char nif
[IFNAMSIZ
] = { 0 };
1026 struct nl80211_msg_conveyor
*req
;
1029 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
1032 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
1034 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
1035 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
1037 nl80211_send(req
, NULL
, NULL
);
1039 snprintf(path
, sizeof(path
) - 1,
1040 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
1042 if ((sysfs
= fopen(path
, "w")) != NULL
)
1044 fwrite("0\n", 1, 2, sysfs
);
1057 static void nl80211_ifdel(const char *ifname
)
1059 struct nl80211_msg_conveyor
*req
;
1062 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1065 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1067 nl80211_send(req
, NULL
, NULL
);
1075 static void nl80211_hostapd_hup(const char *ifname
)
1079 char *phy
= nl80211_ifname2phy(ifname
);
1083 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1084 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1086 if (read(fd
, buf
, sizeof(buf
)) > 0)
1098 static int nl80211_probe(const char *ifname
)
1100 return !!nl80211_ifname2phy(ifname
);
1103 struct nl80211_ssid_bssid
{
1104 unsigned char *ssid
;
1105 unsigned char bssid
[7];
1108 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1110 struct nl80211_ssid_bssid
*sb
= arg
;
1111 struct nlattr
**tb
= nl80211_parse(msg
);
1113 if (tb
[NL80211_ATTR_MAC
]) {
1115 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1116 sizeof(sb
->bssid
) - 1);
1122 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1126 struct nl80211_ssid_bssid
*sb
= arg
;
1127 struct nlattr
**tb
= nl80211_parse(msg
);
1128 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1130 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1131 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1132 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1135 if (!tb
[NL80211_ATTR_BSS
] ||
1136 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1138 !bss
[NL80211_BSS_BSSID
] ||
1139 !bss
[NL80211_BSS_STATUS
] ||
1140 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1145 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1147 case NL80211_BSS_STATUS_ASSOCIATED
:
1148 case NL80211_BSS_STATUS_AUTHENTICATED
:
1149 case NL80211_BSS_STATUS_IBSS_JOINED
:
1153 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1154 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1156 while (ielen
>= 2 && ielen
>= ie
[1])
1160 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1171 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1180 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1183 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1185 /* try to find ssid from scan dump results */
1186 res
= nl80211_phy2ifname(ifname
);
1189 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1190 nl80211_get_ssid_bssid_cb
, &sb
);
1192 /* failed, try to find from hostapd info */
1193 if (sb
.ssid
[0] == 0)
1194 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1195 IWINFO_ESSID_MAX_SIZE
+ 1);
1197 /* failed, try to obtain Mesh ID */
1198 if (sb
.ssid
[0] == 0)
1199 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1200 sb
.ssid
, IWINFO_ESSID_MAX_SIZE
+ 1);
1202 return (sb
.ssid
[0] == 0) ? -1 : 0;
1205 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1207 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1208 struct nl80211_ssid_bssid sb
= { };
1210 res
= nl80211_phy2ifname(ifname
);
1212 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1213 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1214 nl80211_get_macaddr_cb
, &sb
);
1216 /* failed, try to find bssid from scan dump results */
1217 if (sb
.bssid
[0] == 0)
1218 nl80211_request(res
? res
: ifname
,
1219 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1220 nl80211_get_ssid_bssid_cb
, &sb
);
1222 /* failed, try to find mac from hostapd info */
1223 if ((sb
.bssid
[0] == 0) &&
1224 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1227 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1228 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1229 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1230 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1231 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1232 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1237 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1238 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1239 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1248 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1251 struct nlattr
**attr
= nl80211_parse(msg
);
1252 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1254 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1255 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1256 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1259 if (attr
[NL80211_ATTR_BSS
] &&
1260 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1261 attr
[NL80211_ATTR_BSS
], bss_policy
))
1263 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1264 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1270 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1273 struct nlattr
**tb
= nl80211_parse(msg
);
1275 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1276 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1281 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1283 char *res
, channel
[4], hwmode
[3];
1285 /* try to find frequency from interface info */
1286 res
= nl80211_phy2ifname(ifname
);
1289 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1290 nl80211_get_frequency_info_cb
, buf
);
1292 /* failed, try to find frequency from hostapd info */
1294 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1295 "channel", channel
, sizeof(channel
)) == 2)
1297 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1300 /* failed, try to find frequency from scan results */
1303 res
= nl80211_phy2ifname(ifname
);
1305 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1306 nl80211_get_frequency_scan_cb
, buf
);
1309 return (*buf
== 0) ? -1 : 0;
1312 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1315 struct nlattr
**tb
= nl80211_parse(msg
);
1317 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1318 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1323 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1327 /* try to find frequency from interface info */
1328 res
= nl80211_phy2ifname(ifname
);
1331 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1332 nl80211_get_center_freq1_cb
, buf
);
1334 return (*buf
== 0) ? -1 : 0;
1337 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1340 struct nlattr
**tb
= nl80211_parse(msg
);
1342 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1343 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1348 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1352 /* try to find frequency from interface info */
1353 res
= nl80211_phy2ifname(ifname
);
1356 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1357 nl80211_get_center_freq2_cb
, buf
);
1359 return (*buf
== 0) ? -1 : 0;
1362 static int nl80211_get_channel(const char *ifname
, int *buf
)
1364 if (!nl80211_get_frequency(ifname
, buf
))
1366 *buf
= nl80211_freq2channel(*buf
);
1373 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1375 if (!nl80211_get_center_freq1(ifname
, buf
))
1377 *buf
= nl80211_freq2channel(*buf
);
1384 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1386 if (!nl80211_get_center_freq2(ifname
, buf
))
1388 *buf
= nl80211_freq2channel(*buf
);
1395 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1398 struct nlattr
**tb
= nl80211_parse(msg
);
1400 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1401 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1406 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1410 res
= nl80211_phy2ifname(ifname
);
1413 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1414 nl80211_get_txpower_cb
, buf
))
1421 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1425 struct nl80211_rssi_rate
*rr
= arg
;
1426 struct nlattr
**attr
= nl80211_parse(msg
);
1427 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1428 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1430 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1431 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1432 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1433 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1434 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1435 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1436 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1437 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1438 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1439 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1440 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1443 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1444 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1445 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1446 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1447 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1450 if (attr
[NL80211_ATTR_STA_INFO
])
1452 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1453 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1455 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1457 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1458 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1462 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1464 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1465 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1468 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1470 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1471 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1482 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1487 memset(r
, 0, sizeof(*r
));
1489 if ((d
= opendir("/sys/class/net")) != NULL
)
1491 while ((de
= readdir(d
)) != NULL
)
1493 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1494 (!de
->d_name
[strlen(ifname
)] ||
1495 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1497 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1498 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1506 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1508 struct nl80211_rssi_rate rr
;
1510 nl80211_fill_signal(ifname
, &rr
);
1512 if (rr
.rate_samples
)
1514 *buf
= (rr
.rate
* 100);
1521 static int nl80211_get_signal(const char *ifname
, int *buf
)
1523 struct nl80211_rssi_rate rr
;
1525 nl80211_fill_signal(ifname
, &rr
);
1527 if (rr
.rssi_samples
)
1536 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1538 int8_t *noise
= arg
;
1539 struct nlattr
**tb
= nl80211_parse(msg
);
1540 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1542 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1543 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1544 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1547 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1550 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1551 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1554 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1557 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1558 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1564 static int nl80211_get_noise(const char *ifname
, int *buf
)
1568 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1569 nl80211_get_noise_cb
, &noise
))
1580 static int nl80211_get_quality(const char *ifname
, int *buf
)
1584 if (!nl80211_get_signal(ifname
, &signal
))
1586 /* A positive signal level is usually just a quality
1587 * value, pass through as-is */
1593 /* The cfg80211 wext compat layer assumes a signal range
1594 * of -110 dBm to -40 dBm, the quality value is derived
1595 * by adding 110 to the signal level */
1600 else if (signal
> -40)
1603 *buf
= (signal
+ 110);
1612 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1614 /* The cfg80211 wext compat layer assumes a maximum
1621 static int nl80211_check_wepkey(const char *key
)
1625 switch (strlen(key
))
1629 return IWINFO_CIPHER_WEP40
;
1633 return IWINFO_CIPHER_WEP104
;
1644 } wpa_key_mgmt_strings
[] = {
1645 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1646 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1647 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1648 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1649 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1650 { "NONE", 0, IWINFO_KMGMT_NONE
},
1651 { "None", 0, IWINFO_KMGMT_NONE
},
1652 { "PSK", 0, IWINFO_KMGMT_PSK
},
1653 { "EAP", 0, IWINFO_KMGMT_8021x
},
1654 { "SAE", 4, IWINFO_KMGMT_SAE
},
1655 { "OWE", 4, IWINFO_KMGMT_OWE
}
1658 static void parse_wpa_suites(const char *str
, int defversion
,
1659 uint8_t *versions
, uint8_t *suites
)
1663 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1669 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1671 m
= wpa_key_mgmt_strings
[i
].match
;
1674 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1676 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1677 version
= wpa_key_mgmt_strings
[i
].version
;
1679 version
= defversion
;
1681 *versions
|= version
;
1682 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1690 q
+= strcspn(q
, sep
);
1692 p
= q
+ strspn(q
, sep
);
1699 } wpa_cipher_strings
[] = {
1700 { "WEP-104", IWINFO_CIPHER_WEP104
},
1701 { "WEP-40", IWINFO_CIPHER_WEP40
},
1702 { "NONE", IWINFO_CIPHER_NONE
},
1703 { "TKIP", IWINFO_CIPHER_TKIP
},
1704 { "CCMP", IWINFO_CIPHER_CCMP
},
1705 { "GCMP", IWINFO_CIPHER_GCMP
}
1708 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1712 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1718 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1720 m
= wpa_cipher_strings
[i
].match
;
1723 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1725 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1733 q
+= strcspn(q
, sep
);
1735 p
= q
+ strspn(q
, sep
);
1739 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1743 uint8_t wpa_version
= 0;
1744 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1745 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1748 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1750 /* WPA supplicant */
1751 if (nl80211_wpactl_query(ifname
,
1752 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1753 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1754 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1755 "mode", mode
, sizeof(mode
)))
1758 if (!strcmp(wpa_key_mgmt
, "NONE"))
1760 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1761 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1763 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1765 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1766 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1769 c
->pair_ciphers
= 0;
1770 c
->group_ciphers
= 0;
1775 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1779 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1780 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1781 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1787 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1788 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1792 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1797 else if (!strncmp(p
, "WPA-", 4))
1803 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1805 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1812 else if (nl80211_hostapd_query(ifname
,
1813 "wpa", wpa
, sizeof(wpa
),
1814 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1815 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1816 "auth_algs", auth_algs
, sizeof(auth_algs
),
1817 "wep_key0", wep_key0
, sizeof(wep_key0
),
1818 "wep_key1", wep_key1
, sizeof(wep_key1
),
1819 "wep_key2", wep_key2
, sizeof(wep_key2
),
1820 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1824 if (wpa_key_mgmt
[0])
1826 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1828 if (!strncmp(p
, "WPA-", 4))
1831 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1834 c
->enabled
= c
->wpa_version
? 1 : 0;
1837 if (wpa_pairwise
[0])
1838 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1842 switch (atoi(auth_algs
))
1845 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1849 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1853 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1854 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1858 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1859 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1860 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1861 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1863 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1866 c
->group_ciphers
= c
->pair_ciphers
;
1871 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
1872 else if (!nl80211_get_mode(ifname
, &opmode
) &&
1873 (opmode
== IWINFO_OPMODE_ADHOC
||
1874 opmode
== IWINFO_OPMODE_MESHPOINT
))
1885 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1889 name
= nl80211_ifname2phy(ifname
);
1896 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1898 name
= nl80211_ifname2phy(name
);
1902 strcpy(buf
, ifname
);
1911 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1912 struct iwinfo_rate_entry
*re
)
1914 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1915 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1916 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1917 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1919 if (ri
[NL80211_RATE_INFO_HE_MCS
])
1922 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_MCS
]);
1924 if (ri
[NL80211_RATE_INFO_HE_NSS
])
1925 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_NSS
]);
1926 if (ri
[NL80211_RATE_INFO_HE_GI
])
1927 re
->he_gi
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_GI
]);
1928 if (ri
[NL80211_RATE_INFO_HE_DCM
])
1929 re
->he_dcm
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_DCM
]);
1931 else if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1934 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1936 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1937 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1939 else if (ri
[NL80211_RATE_INFO_MCS
])
1942 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1945 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1947 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1949 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1951 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1953 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1954 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
1959 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
1960 re
->is_short_gi
= 1;
1962 re
->is_40mhz
= (re
->mhz
== 40);
1965 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
1967 struct nl80211_array_buf
*arr
= arg
;
1968 struct iwinfo_survey_entry
*e
= arr
->buf
;
1969 struct nlattr
**attr
= nl80211_parse(msg
);
1970 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
1973 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1974 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1975 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1976 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
1977 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
1978 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
1979 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
1980 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
1983 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
1984 attr
[NL80211_ATTR_SURVEY_INFO
],
1989 /* advance to end of array */
1991 memset(e
, 0, sizeof(*e
));
1993 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
1994 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
1996 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
1997 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
1999 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
2000 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
2002 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
2003 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
2005 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
2006 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
2008 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
2009 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
2011 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
2012 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
2019 static void plink_state_to_str(char *dst
, unsigned state
)
2022 case NL80211_PLINK_LISTEN
:
2023 strcpy(dst
, "LISTEN");
2025 case NL80211_PLINK_OPN_SNT
:
2026 strcpy(dst
, "OPN_SNT");
2028 case NL80211_PLINK_OPN_RCVD
:
2029 strcpy(dst
, "OPN_RCVD");
2031 case NL80211_PLINK_CNF_RCVD
:
2032 strcpy(dst
, "CNF_RCVD");
2034 case NL80211_PLINK_ESTAB
:
2035 strcpy(dst
, "ESTAB");
2037 case NL80211_PLINK_HOLDING
:
2038 strcpy(dst
, "HOLDING");
2040 case NL80211_PLINK_BLOCKED
:
2041 strcpy(dst
, "BLOCKED");
2044 strcpy(dst
, "UNKNOWN");
2049 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
2051 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
2054 case NL80211_MESH_POWER_ACTIVE
:
2055 strcpy(dst
, "ACTIVE");
2057 case NL80211_MESH_POWER_LIGHT_SLEEP
:
2058 strcpy(dst
, "LIGHT SLEEP");
2060 case NL80211_MESH_POWER_DEEP_SLEEP
:
2061 strcpy(dst
, "DEEP SLEEP");
2064 strcpy(dst
, "UNKNOWN");
2069 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2071 struct nl80211_array_buf
*arr
= arg
;
2072 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2073 struct nlattr
**attr
= nl80211_parse(msg
);
2074 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2075 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2076 struct nl80211_sta_flag_update
*sta_flags
;
2078 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2079 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2080 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2081 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2082 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2083 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2084 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2085 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2086 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2087 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2088 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2089 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2090 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2091 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2092 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2093 [NL80211_STA_INFO_STA_FLAGS
] =
2094 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2095 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2097 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2098 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2099 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2100 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2101 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2102 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2105 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2106 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2107 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2108 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2109 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2112 /* advance to end of array */
2114 memset(e
, 0, sizeof(*e
));
2116 if (attr
[NL80211_ATTR_MAC
])
2117 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2119 if (attr
[NL80211_ATTR_STA_INFO
] &&
2120 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2121 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2123 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2124 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2126 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2127 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2129 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2130 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2132 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2133 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2135 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2136 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2138 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2139 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2141 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2142 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2143 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2144 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2146 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2147 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2148 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2149 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2151 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2152 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2154 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2155 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2157 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2158 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2160 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2161 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2163 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2164 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2166 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2167 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2169 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2170 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2173 if (sinfo
[NL80211_STA_INFO_LLID
])
2174 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2176 if (sinfo
[NL80211_STA_INFO_PLID
])
2177 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2179 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2180 plink_state_to_str(e
->plink_state
,
2181 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2183 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2184 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2185 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2186 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2187 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2188 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2191 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2193 sta_flags
= (struct nl80211_sta_flag_update
*)
2194 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2196 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2197 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2198 e
->is_authorized
= 1;
2200 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2201 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2202 e
->is_authenticated
= 1;
2204 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2205 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2206 e
->is_preamble_short
= 1;
2208 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2209 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2212 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2213 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2216 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2217 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2222 e
->noise
= 0; /* filled in by caller */
2228 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2230 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2233 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2234 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2236 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2243 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2248 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2249 struct iwinfo_assoclist_entry
*e
;
2251 if ((d
= opendir("/sys/class/net")) != NULL
)
2253 while ((de
= readdir(d
)) != NULL
)
2255 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2256 (!de
->d_name
[strlen(ifname
)] ||
2257 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2259 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2260 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2266 if (!nl80211_get_noise(ifname
, &noise
))
2267 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2270 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2277 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2280 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2282 struct nlattr
**attr
= nl80211_parse(msg
);
2283 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2284 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2285 struct nlattr
*band
, *freq
;
2287 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2288 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2289 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2290 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2291 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2292 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2293 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2296 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2299 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2301 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2302 nla_len(band
), NULL
);
2304 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2306 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2307 nla_data(freq
), nla_len(freq
), freq_policy
);
2309 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2310 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2312 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2313 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2315 *dbm_max
= (int)(0.01 * nla_get_u32(
2316 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2326 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2329 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2330 struct nl80211_msg_conveyor
*req
;
2331 struct iwinfo_txpwrlist_entry entry
;
2333 if (nl80211_get_channel(ifname
, &ch_cur
))
2336 /* initialize the value pointer with channel for callback */
2339 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2340 nl80211_get_txpwrlist_cb
, &dbm_max
);
2344 for (dbm_cur
= 0, dbm_cnt
= 0;
2346 dbm_cur
++, dbm_cnt
++)
2348 entry
.dbm
= dbm_cur
;
2349 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2351 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2354 entry
.dbm
= dbm_max
;
2355 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2357 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2360 *len
= dbm_cnt
* sizeof(entry
);
2367 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2369 int wpa_version
= 0;
2370 char *p
, *q
, *proto
, *suites
;
2374 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2375 if (!strcmp(p
, "WEP")) {
2377 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2378 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2379 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2383 proto
= strtok(p
, "-");
2384 suites
= strtok(NULL
, "]");
2386 if (!proto
|| !suites
)
2389 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2391 else if (!strcmp(proto
, "WPA"))
2398 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2399 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2404 struct nl80211_scanlist
{
2405 struct iwinfo_scanlist_entry
*e
;
2410 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2411 struct iwinfo_scanlist_entry
*e
)
2413 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2414 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2415 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2418 while (ielen
>= 2 && ielen
>= ie
[1])
2423 case 114: /* Mesh ID */
2424 if (e
->ssid
[0] == 0) {
2425 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2426 memcpy(e
->ssid
, ie
+ 2, len
);
2432 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2433 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2436 case 221: /* Vendor */
2437 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2438 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2439 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2441 case 61: /* HT oeration */
2443 e
->ht_chan_info
.primary_chan
= ie
[2];
2444 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2445 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2448 case 192: /* VHT operation */
2450 e
->vht_chan_info
.chan_width
= ie
[2];
2451 e
->vht_chan_info
.center_chan_1
= ie
[3];
2452 e
->vht_chan_info
.center_chan_2
= ie
[4];
2462 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2467 struct nl80211_scanlist
*sl
= arg
;
2468 struct nlattr
**tb
= nl80211_parse(msg
);
2469 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2471 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2472 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2473 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2474 [NL80211_BSS_BSSID
] = { 0 },
2475 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2476 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2477 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2478 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2479 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2480 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2481 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2482 [NL80211_BSS_BEACON_IES
] = { 0 },
2485 if (!tb
[NL80211_ATTR_BSS
] ||
2486 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2488 !bss
[NL80211_BSS_BSSID
])
2493 if (bss
[NL80211_BSS_CAPABILITY
])
2494 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2498 memset(sl
->e
, 0, sizeof(*sl
->e
));
2499 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2502 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2503 else if (caps
& (1<<0))
2504 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2506 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2509 sl
->e
->crypto
.enabled
= 1;
2511 if (bss
[NL80211_BSS_FREQUENCY
])
2512 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2513 bss
[NL80211_BSS_FREQUENCY
]));
2515 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2516 nl80211_get_scanlist_ie(bss
, sl
->e
);
2518 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2521 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2523 rssi
= sl
->e
->signal
- 0x100;
2527 else if (rssi
> -40)
2530 sl
->e
->quality
= (rssi
+ 110);
2531 sl
->e
->quality_max
= 70;
2534 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2536 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2537 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2546 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2548 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2550 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2553 if (nl80211_wait("nl80211", "scan",
2554 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2557 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2558 nl80211_get_scanlist_cb
, &sl
))
2561 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2569 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2572 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2573 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2579 if (len
+ 1 >= outlen
)
2589 out
[len
++] = '\n'; in
++;
2593 out
[len
++] = '\r'; in
++;
2597 out
[len
++] = '\t'; in
++;
2601 out
[len
++] = '\033'; in
++;
2605 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2606 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2628 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2630 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2631 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2632 struct sockaddr_un local
= { 0 };
2633 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2635 sock
= nl80211_wpactl_connect(ifname
, &local
);
2640 send(sock
, "ATTACH", 6, 0);
2641 send(sock
, "SCAN", 4, 0);
2644 * wait for scan results:
2645 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2646 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2647 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2648 * time to process the results, so try 72 + 2 + 1 times.
2650 for (tries
= 0; tries
< 75; tries
++)
2652 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2655 /* got an event notification */
2656 if (reply
[0] == '<')
2658 /* scan results are ready */
2659 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2661 /* send "SCAN_RESULTS" command */
2662 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2666 /* is another unrelated event, retry */
2670 /* scanning already in progress, keep awaiting results */
2671 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2676 /* another failure, abort */
2677 else if (!strncmp(reply
, "FAIL-", 5))
2683 /* receive and parse scan results if the wait above didn't time out */
2684 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2686 /* received an event notification, receive again */
2687 if (reply
[0] == '<')
2690 nl80211_get_quality_max(ifname
, &qmax
);
2692 for (line
= strtok_r(reply
, "\n", &pos
);
2694 line
= strtok_r(NULL
, "\n", &pos
))
2696 /* skip header line */
2703 bssid
= strtok(line
, "\t");
2704 freq
= strtok(NULL
, "\t");
2705 signal
= strtok(NULL
, "\t");
2706 flags
= strtok(NULL
, "\t");
2707 ssid
= strtok(NULL
, "\n");
2709 if (!bssid
|| !freq
|| !signal
|| !flags
)
2713 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2714 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2715 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2716 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2717 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2718 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2722 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2727 if (strstr(flags
, "[MESH]"))
2728 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2729 else if (strstr(flags
, "[IBSS]"))
2730 e
->mode
= IWINFO_OPMODE_ADHOC
;
2732 e
->mode
= IWINFO_OPMODE_MASTER
;
2735 e
->channel
= nl80211_freq2channel(atoi(freq
));
2738 rssi
= atoi(signal
);
2744 /* The cfg80211 wext compat layer assumes a signal range
2745 * of -110 dBm to -40 dBm, the quality value is derived
2746 * by adding 110 to the signal level */
2749 else if (rssi
> -40)
2752 e
->quality
= (rssi
+ 110);
2760 e
->quality_max
= qmax
;
2763 nl80211_get_scancrypto(flags
, &e
->crypto
);
2769 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2774 unlink(local
.sun_path
);
2776 return (count
>= 0) ? 0 : -1;
2779 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2786 /* Got a radioX pseudo interface, find some interface on it or create one */
2787 if (!strncmp(ifname
, "radio", 5))
2789 /* Reuse existing interface */
2790 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2792 return nl80211_get_scanlist(res
, buf
, len
);
2795 /* Need to spawn a temporary iface for scanning */
2796 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2798 rv
= nl80211_get_scanlist(res
, buf
, len
);
2804 /* WPA supplicant */
2805 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2810 /* station / ad-hoc / monitor scan */
2811 else if (!nl80211_get_mode(ifname
, &mode
) &&
2812 (mode
== IWINFO_OPMODE_ADHOC
||
2813 mode
== IWINFO_OPMODE_MASTER
||
2814 mode
== IWINFO_OPMODE_CLIENT
||
2815 mode
== IWINFO_OPMODE_MONITOR
) &&
2816 iwinfo_ifup(ifname
))
2818 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2824 /* Got a temp interface, don't create yet another one */
2825 if (!strncmp(ifname
, "tmp.", 4))
2827 if (!iwinfo_ifup(ifname
))
2830 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2831 iwinfo_ifdown(ifname
);
2835 /* Spawn a new scan interface */
2838 if (!(res
= nl80211_ifadd(ifname
)))
2843 /* if we can take the new interface up, the driver supports an
2844 * additional interface and there's no need to tear down the ap */
2845 if (iwinfo_ifup(res
))
2847 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2851 /* driver cannot create secondary interface, take down ap
2853 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2855 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2857 iwinfo_ifup(ifname
);
2858 nl80211_hostapd_hup(ifname
);
2869 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2871 int bands_remain
, freqs_remain
;
2873 struct nl80211_array_buf
*arr
= arg
;
2874 struct iwinfo_freqlist_entry
*e
;
2876 struct nlattr
**attr
= nl80211_parse(msg
);
2877 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2878 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2879 struct nlattr
*band
, *freq
;
2884 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
2885 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2887 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2888 nla_data(band
), nla_len(band
), NULL
);
2890 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
2891 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2893 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2894 nla_data(freq
), nla_len(freq
), NULL
);
2896 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2897 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2900 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2901 e
->channel
= nl80211_freq2channel(e
->mhz
);
2904 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2905 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2908 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
2909 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
2910 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
2911 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
2912 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
2913 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
2914 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
2915 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
2916 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
2917 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
2918 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
2919 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
2931 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2933 struct nl80211_msg_conveyor
*cv
;
2934 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2935 uint32_t features
= nl80211_get_protocol_features(ifname
);
2938 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
2939 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
2943 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
2944 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
2947 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
2957 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
2960 struct nlattr
**attr
= nl80211_parse(msg
);
2962 if (attr
[NL80211_ATTR_REG_ALPHA2
])
2963 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
2970 static int nl80211_get_country(const char *ifname
, char *buf
)
2972 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
2973 nl80211_get_country_cb
, buf
))
2979 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
2982 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
2983 const struct iwinfo_iso3166_label
*l
;
2985 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
2987 e
->iso3166
= l
->iso3166
;
2988 e
->ccode
[0] = (l
->iso3166
/ 256);
2989 e
->ccode
[1] = (l
->iso3166
% 256);
2993 *len
= (count
* sizeof(struct iwinfo_country_entry
));
2998 struct nl80211_modes
3007 uint16_t he_phy_cap
[6];
3010 static int nl80211_eval_modelist(struct nl80211_modes
*m
)
3012 /* Treat any nonzero capability as 11n */
3015 m
->hw
|= IWINFO_80211_N
;
3016 m
->ht
|= IWINFO_HTMODE_HT20
;
3018 if (m
->nl_ht
& (1 << 1))
3019 m
->ht
|= IWINFO_HTMODE_HT40
;
3022 if (m
->he_phy_cap
[0] != 0) {
3023 m
->hw
|= IWINFO_80211_AX
;
3024 m
->ht
|= IWINFO_HTMODE_HE20
;
3026 if (m
->he_phy_cap
[0] & BIT(9))
3027 m
->ht
|= IWINFO_HTMODE_HE40
;
3028 if (m
->he_phy_cap
[0] & BIT(10))
3029 m
->ht
|= IWINFO_HTMODE_HE40
| IWINFO_HTMODE_HE80
;
3030 if (m
->he_phy_cap
[0] & BIT(11))
3031 m
->ht
|= IWINFO_HTMODE_HE160
;
3032 if (m
->he_phy_cap
[0] & BIT(12))
3033 m
->ht
|= IWINFO_HTMODE_HE160
| IWINFO_HTMODE_HE80_80
;
3036 if (m
->nl_freq
< 2485)
3038 m
->hw
|= IWINFO_80211_B
;
3039 m
->hw
|= IWINFO_80211_G
;
3043 /* Treat any nonzero capability as 11ac */
3046 m
->hw
|= IWINFO_80211_AC
;
3047 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3049 switch ((m
->nl_vht
>> 2) & 3)
3052 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3056 m
->ht
|= IWINFO_HTMODE_VHT160
;
3060 else if (m
->nl_freq
>= 56160)
3062 m
->hw
|= IWINFO_80211_AD
;
3064 else if (!(m
->hw
& IWINFO_80211_AC
))
3066 m
->hw
|= IWINFO_80211_A
;
3070 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
3072 struct nl80211_modes
*m
= arg
;
3073 int bands_remain
, freqs_remain
;
3075 uint32_t vht_caps
= 0;
3076 struct nlattr
**attr
= nl80211_parse(msg
);
3077 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3078 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
3079 struct nlattr
*band
, *freq
;
3081 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
3083 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3085 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3086 nla_data(band
), nla_len(band
), NULL
);
3088 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
3089 m
->nl_ht
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
3091 if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
3092 m
->nl_vht
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
3094 if (bands
[NL80211_BAND_ATTR_IFTYPE_DATA
]) {
3095 struct nlattr
*tb
[NL80211_BAND_IFTYPE_ATTR_MAX
+ 1];
3096 struct nlattr
*nl_iftype
;
3100 nla_for_each_nested(nl_iftype
, bands
[NL80211_BAND_ATTR_IFTYPE_DATA
], rem_band
) {
3101 nla_parse(tb
, NL80211_BAND_IFTYPE_ATTR_MAX
,
3102 nla_data(nl_iftype
), nla_len(nl_iftype
), NULL
);
3103 if (tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]) {
3104 len
= nla_len(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]);
3106 if (len
> sizeof(m
->he_phy_cap
) - 1)
3107 len
= sizeof(m
->he_phy_cap
) - 1;
3108 memcpy(&((__u8
*)m
->he_phy_cap
)[1],
3109 nla_data(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]),
3115 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
3116 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
3119 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
3120 nla_data(freq
), nla_len(freq
), NULL
);
3122 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
3125 m
->nl_freq
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
3136 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3138 struct nl80211_msg_conveyor
*cv
;
3139 struct nl80211_modes m
= {};
3140 uint32_t features
= nl80211_get_protocol_features(ifname
);
3143 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3144 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3148 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3149 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3150 goto nla_put_failure
;
3152 nl80211_eval_modelist(&m
);
3169 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3171 struct nlattr
**tb
= nl80211_parse(msg
);
3173 struct chan_info
*chn
= arg
;
3175 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3176 chn
->width
= nla_get_u32(cur
);
3178 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3179 chn
->mode
= nla_get_u32(cur
);
3184 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3186 struct chan_info chn
= { .width
= 0, .mode
= 0 };
3190 res
= nl80211_phy2ifname(ifname
);
3193 err
= nl80211_request(res
? res
: ifname
,
3194 NL80211_CMD_GET_INTERFACE
, 0,
3195 nl80211_get_htmode_cb
, &chn
);
3199 switch (chn
.width
) {
3200 case NL80211_CHAN_WIDTH_20
:
3202 *buf
= IWINFO_HTMODE_VHT20
;
3204 *buf
= IWINFO_HTMODE_HT20
;
3206 case NL80211_CHAN_WIDTH_40
:
3208 *buf
= IWINFO_HTMODE_VHT40
;
3210 *buf
= IWINFO_HTMODE_HT40
;
3212 case NL80211_CHAN_WIDTH_80
:
3213 *buf
= IWINFO_HTMODE_VHT80
;
3215 case NL80211_CHAN_WIDTH_80P80
:
3216 *buf
= IWINFO_HTMODE_VHT80_80
;
3218 case NL80211_CHAN_WIDTH_160
:
3219 *buf
= IWINFO_HTMODE_VHT160
;
3221 case NL80211_CHAN_WIDTH_5
:
3222 case NL80211_CHAN_WIDTH_10
:
3223 case NL80211_CHAN_WIDTH_20_NOHT
:
3224 *buf
= IWINFO_HTMODE_NOHT
;
3233 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3235 struct nl80211_msg_conveyor
*cv
;
3236 struct nl80211_modes m
= {};
3237 uint32_t features
= nl80211_get_protocol_features(ifname
);
3240 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3241 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3245 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3246 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3247 goto nla_put_failure
;
3249 nl80211_eval_modelist(&m
);
3262 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3264 struct nlattr
**attr
= nl80211_parse(msg
);
3265 struct nlattr
*comb
;
3267 int comb_rem
, limit_rem
, mode_rem
;
3270 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3273 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3275 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3276 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3277 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3279 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3280 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3281 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3282 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3284 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3285 struct nlattr
*limit
;
3287 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3289 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3292 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3294 struct nlattr
*mode
;
3296 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3298 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3299 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3302 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3305 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3306 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3315 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3317 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3318 nl80211_get_ifcomb_cb
, buf
))
3324 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3326 char *phy
, compat
[64], path
[PATH_MAX
];
3329 /* Try to determine the phy name from the given interface */
3330 phy
= nl80211_ifname2phy(ifname
);
3332 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3333 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3335 if (nl80211_readstr(path
, compat
, sizeof(compat
)) <= 0)
3338 if (!strcmp(compat
, "qca,ar9130-wmac")) {
3339 id
->vendor_id
= 0x168c;
3340 id
->device_id
= 0x0029;
3341 id
->subsystem_vendor_id
= 0x168c;
3342 id
->subsystem_device_id
= 0x9130;
3343 } else if (!strcmp(compat
, "qca,ar9330-wmac")) {
3344 id
->vendor_id
= 0x168c;
3345 id
->device_id
= 0x0030;
3346 id
->subsystem_vendor_id
= 0x168c;
3347 id
->subsystem_device_id
= 0x9330;
3348 } else if (!strcmp(compat
, "qca,ar9340-wmac")) {
3349 id
->vendor_id
= 0x168c;
3350 id
->device_id
= 0x0030;
3351 id
->subsystem_vendor_id
= 0x168c;
3352 id
->subsystem_device_id
= 0x9340;
3353 } else if (!strcmp(compat
, "qca,qca9530-wmac")) {
3354 id
->vendor_id
= 0x168c;
3355 id
->device_id
= 0x0033;
3356 id
->subsystem_vendor_id
= 0x168c;
3357 id
->subsystem_device_id
= 0x9530;
3358 } else if (!strcmp(compat
, "qca,qca9550-wmac")) {
3359 id
->vendor_id
= 0x168c;
3360 id
->device_id
= 0x0033;
3361 id
->subsystem_vendor_id
= 0x168c;
3362 id
->subsystem_device_id
= 0x9550;
3363 } else if (!strcmp(compat
, "qca,qca9560-wmac")) {
3364 id
->vendor_id
= 0x168c;
3365 id
->device_id
= 0x0033;
3366 id
->subsystem_vendor_id
= 0x168c;
3367 id
->subsystem_device_id
= 0x9560;
3368 } else if (!strcmp(compat
, "qcom,ipq4019-wifi")) {
3369 id
->vendor_id
= 0x168c;
3370 id
->device_id
= 0x003c;
3371 id
->subsystem_vendor_id
= 0x168c;
3372 id
->subsystem_device_id
= 0x4019;
3373 } else if (!strcmp(compat
, "mediatek,mt7622-wmac")) {
3374 id
->vendor_id
= 0x14c3;
3375 id
->device_id
= 0x7622;
3376 id
->subsystem_vendor_id
= 0x14c3;
3377 id
->subsystem_device_id
= 0x7622;
3379 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
3383 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3385 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3386 char *phy
, num
[8], path
[PATH_MAX
];
3389 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3390 { "vendor", &id
->vendor_id
},
3391 { "device", &id
->device_id
},
3392 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3393 { "subsystem_device", &id
->subsystem_device_id
}
3396 memset(id
, 0, sizeof(*id
));
3398 /* Try to determine the phy name from the given interface */
3399 phy
= nl80211_ifname2phy(ifname
);
3401 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3403 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3404 phy
? "ieee80211" : "net",
3405 phy
? phy
: ifname
, lookup
[i
].path
);
3407 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3408 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3411 /* Failed to obtain hardware IDs, try FDT */
3412 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3413 if (!nl80211_hardware_id_from_fdt(id
, ifname
))
3416 /* Failed to obtain hardware IDs, search board config */
3417 if (id
->vendor_id
== 0 || id
->device_id
== 0)
3418 return iwinfo_hardware_id_from_mtd(id
);
3423 static const struct iwinfo_hardware_entry
*
3424 nl80211_get_hardware_entry(const char *ifname
)
3426 struct iwinfo_hardware_id id
;
3428 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3431 return iwinfo_hardware(&id
);
3434 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3436 const struct iwinfo_hardware_entry
*hw
;
3438 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3439 sprintf(buf
, "Generic MAC80211");
3441 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3446 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3448 const struct iwinfo_hardware_entry
*hw
;
3450 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3453 *buf
= hw
->txpower_offset
;
3457 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3459 const struct iwinfo_hardware_entry
*hw
;
3461 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3464 *buf
= hw
->frequency_offset
;
3468 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3472 if ((idx
= nl80211_phy_idx_from_uci(section
)) < 0)
3475 sprintf(buf
, "phy%d", idx
);
3479 static int nl80211_phy_path(const char *phyname
, const char **path
)
3481 if (strncmp(phyname
, "phy", 3) != 0)
3484 if (strchr(phyname
, '/'))
3487 *path
= nl80211_phy_path_str(phyname
);
3494 const struct iwinfo_ops nl80211_ops
= {
3496 .probe
= nl80211_probe
,
3497 .channel
= nl80211_get_channel
,
3498 .center_chan1
= nl80211_get_center_chan1
,
3499 .center_chan2
= nl80211_get_center_chan2
,
3500 .frequency
= nl80211_get_frequency
,
3501 .frequency_offset
= nl80211_get_frequency_offset
,
3502 .txpower
= nl80211_get_txpower
,
3503 .txpower_offset
= nl80211_get_txpower_offset
,
3504 .bitrate
= nl80211_get_bitrate
,
3505 .signal
= nl80211_get_signal
,
3506 .noise
= nl80211_get_noise
,
3507 .quality
= nl80211_get_quality
,
3508 .quality_max
= nl80211_get_quality_max
,
3509 .mbssid_support
= nl80211_get_mbssid_support
,
3510 .hwmodelist
= nl80211_get_hwmodelist
,
3511 .htmodelist
= nl80211_get_htmodelist
,
3512 .htmode
= nl80211_get_htmode
,
3513 .mode
= nl80211_get_mode
,
3514 .ssid
= nl80211_get_ssid
,
3515 .bssid
= nl80211_get_bssid
,
3516 .country
= nl80211_get_country
,
3517 .hardware_id
= nl80211_get_hardware_id
,
3518 .hardware_name
= nl80211_get_hardware_name
,
3519 .encryption
= nl80211_get_encryption
,
3520 .phyname
= nl80211_get_phyname
,
3521 .assoclist
= nl80211_get_assoclist
,
3522 .txpwrlist
= nl80211_get_txpwrlist
,
3523 .scanlist
= nl80211_get_scanlist
,
3524 .freqlist
= nl80211_get_freqlist
,
3525 .countrylist
= nl80211_get_countrylist
,
3526 .survey
= nl80211_get_survey
,
3527 .lookup_phy
= nl80211_lookup_phyname
,
3528 .phy_path
= nl80211_phy_path
,
3529 .close
= nl80211_close