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')
156 static int nl80211_get_band(int nla_type
)
160 case NL80211_BAND_2GHZ
:
161 return IWINFO_BAND_24
;
162 case NL80211_BAND_5GHZ
:
163 return IWINFO_BAND_5
;
164 case NL80211_BAND_6GHZ
:
165 return IWINFO_BAND_6
;
166 case NL80211_BAND_60GHZ
:
167 return IWINFO_BAND_60
;
174 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
175 struct nlmsgerr
*err
, void *arg
)
182 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
189 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
196 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
201 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
216 static struct nl80211_msg_conveyor
* nl80211_new(struct genl_family
*family
,
219 static struct nl80211_msg_conveyor cv
;
221 struct nl_msg
*req
= NULL
;
222 struct nl_cb
*cb
= NULL
;
228 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
232 genlmsg_put(req
, 0, 0, genl_family_get_id(family
), 0, flags
, cmd
, 0);
246 static struct nl80211_msg_conveyor
* nl80211_ctl(int cmd
, int flags
)
248 if (nl80211_init() < 0)
251 return nl80211_new(nls
->nlctrl
, cmd
, flags
);
254 static const char *nl80211_phy_path_str(const char *phyname
)
256 static char path
[PATH_MAX
];
257 const char *prefix
= "/sys/devices/";
258 int prefix_len
= strlen(prefix
);
261 char buf
[512], *link
;
266 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", phyname
);
267 phy_idx
= nl80211_readint(buf
);
271 buf_len
= snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/device", phyname
);
272 link
= realpath(buf
, path
);
276 if (strncmp(link
, prefix
, prefix_len
) != 0)
281 prefix
= "platform/";
282 prefix_len
= strlen(prefix
);
283 if (!strncmp(link
, prefix
, prefix_len
) && strstr(link
, "/pci"))
286 snprintf(buf
+ buf_len
, sizeof(buf
) - buf_len
, "/ieee80211");
291 while ((e
= readdir(d
)) != NULL
) {
294 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
295 cur_idx
= nl80211_readint(buf
);
299 if (cur_idx
>= phy_idx
)
310 offset
= link
- path
+ strlen(link
);
311 snprintf(path
+ offset
, sizeof(path
) - offset
, "+%d", seq
);
316 static int nl80211_phy_idx_from_path(const char *path
)
320 const char *cur_path
;
329 path_len
= strlen(path
);
333 d
= opendir("/sys/class/ieee80211");
337 while ((e
= readdir(d
)) != NULL
) {
338 cur_path
= nl80211_phy_path_str(e
->d_name
);
342 cur_path_len
= strlen(cur_path
);
343 if (cur_path_len
< path_len
)
346 if (strcmp(cur_path
+ cur_path_len
- path_len
, path
) != 0)
349 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", e
->d_name
);
350 idx
= nl80211_readint(buf
);
361 static int nl80211_phy_idx_from_macaddr(const char *opt
)
370 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*");
371 if (glob(buf
, 0, NULL
, &gl
))
374 for (i
= 0; i
< gl
.gl_pathc
; i
++)
376 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
377 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
380 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
383 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
384 if ((idx
= nl80211_readint(buf
)) > -1)
393 static int nl80211_phy_idx_from_phy(const char *opt
)
400 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
401 return nl80211_readint(buf
);
404 static int nl80211_phy_idx_from_uci(const char *name
)
406 struct uci_section
*s
;
410 s
= iwinfo_uci_get_radio(name
, "mac80211");
414 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
415 idx
= nl80211_phy_idx_from_path(opt
);
419 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
420 idx
= nl80211_phy_idx_from_macaddr(opt
);
424 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
425 idx
= nl80211_phy_idx_from_phy(opt
);
432 static bool nl80211_is_ifname(const char *name
)
435 char buffer
[PATH_MAX
];
437 snprintf(buffer
, sizeof(buffer
), "/sys/class/net/%s", name
);
438 return !lstat(buffer
, &st
);
441 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
444 unsigned int ifidx
= 0;
446 struct nl80211_msg_conveyor
*cv
;
451 if (nl80211_init() < 0)
454 if (!strncmp(ifname
, "mon.", 4))
455 ifidx
= if_nametoindex(&ifname
[4]);
456 else if (nl80211_is_ifname(ifname
))
457 ifidx
= if_nametoindex(ifname
);
460 phyidx
= nl80211_phy_idx_from_phy(ifname
);
462 phyidx
= nl80211_phy_idx_from_uci(ifname
);
465 /* Valid ifidx must be greater than 0 */
466 if ((ifidx
<= 0) && (phyidx
< 0))
469 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
474 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
475 else if (phyidx
> -1)
476 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
485 static int nl80211_send(struct nl80211_msg_conveyor
*cv
,
486 int (*cb_func
)(struct nl_msg
*, void *),
489 static struct nl80211_msg_conveyor rcv
;
493 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
495 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
497 err
= nl_send_auto_complete(nls
->nl_sock
, cv
->msg
);
504 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
505 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
506 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
509 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
516 static int nl80211_request(const char *ifname
, int cmd
, int flags
,
517 int (*cb_func
)(struct nl_msg
*, void *),
520 struct nl80211_msg_conveyor
*cv
;
522 cv
= nl80211_msg(ifname
, cmd
, flags
);
527 return nl80211_send(cv
, cb_func
, cb_arg
);
530 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
532 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
533 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
535 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
536 genlmsg_attrlen(gnlh
, 0), NULL
);
541 static int nl80211_get_protocol_features_cb(struct nl_msg
*msg
, void *arg
)
543 uint32_t *features
= arg
;
544 struct nlattr
**attr
= nl80211_parse(msg
);
546 if (attr
[NL80211_ATTR_PROTOCOL_FEATURES
])
547 *features
= nla_get_u32(attr
[NL80211_ATTR_PROTOCOL_FEATURES
]);
552 static int nl80211_get_protocol_features(const char *ifname
)
554 struct nl80211_msg_conveyor
*req
;
555 uint32_t features
= 0;
557 req
= nl80211_msg(ifname
, NL80211_CMD_GET_PROTOCOL_FEATURES
, 0);
559 nl80211_send(req
, nl80211_get_protocol_features_cb
, &features
);
566 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
568 struct nl80211_group_conveyor
*cv
= arg
;
570 struct nlattr
**attr
= nl80211_parse(msg
);
571 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
575 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
578 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
580 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
581 nla_data(mgrp
), nla_len(mgrp
), NULL
);
583 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
584 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
585 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
586 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
588 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
596 static int nl80211_subscribe(const char *family
, const char *group
)
598 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
599 struct nl80211_msg_conveyor
*req
;
602 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
605 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
606 err
= nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
611 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
621 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
623 struct nl80211_event_conveyor
*cv
= arg
;
624 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
626 if (cv
->wait
[gnlh
->cmd
/ 32] & (1 << (gnlh
->cmd
% 32)))
627 cv
->recv
= gnlh
->cmd
;
632 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
637 static int __nl80211_wait(const char *family
, const char *group
, ...)
639 struct nl80211_event_conveyor cv
= { };
645 if (nl80211_subscribe(family
, group
))
648 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
653 nl_cb_err(cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
654 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
655 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
659 for (cmd
= va_arg(ap
, int); cmd
!= 0; cmd
= va_arg(ap
, int))
660 cv
.wait
[cmd
/ 32] |= (1 << (cmd
% 32));
664 while (!cv
.recv
&& !err
)
665 nl_recvmsgs(nls
->nl_sock
, cb
);
672 #define nl80211_wait(family, group, ...) \
673 __nl80211_wait(family, group, __VA_ARGS__, 0)
675 /* This is linux's ieee80211_freq_khz_to_channel() which is:
676 * SPDX-License-Identifier: GPL-2.0
677 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
678 * Copyright 2013-2014 Intel Mobile Communications GmbH
679 * Copyright 2017 Intel Deutschland GmbH
680 * Copyright (C) 2018-2022 Intel Corporation
682 static int nl80211_freq2channel(int freq
)
686 else if (freq
< 2484)
687 return (freq
- 2407) / 5;
688 else if (freq
>= 4910 && freq
<= 4980)
689 return (freq
- 4000) / 5;
690 else if (freq
< 5925)
691 return (freq
- 5000) / 5;
692 else if (freq
== 5935)
694 else if (freq
<= 45000) /* DMG band lower limit */
695 /* see 802.11ax D6.1 27.3.22.2 */
696 return (freq
- 5950) / 5;
697 else if (freq
>= 58320 && freq
<= 70200)
698 return (freq
- 56160) / 2160;
703 /* This is linux's ieee80211_channel_to_freq_khz() which is:
704 * SPDX-License-Identifier: GPL-2.0
705 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
706 * Copyright 2013-2014 Intel Mobile Communications GmbH
707 * Copyright 2017 Intel Deutschland GmbH
708 * Copyright (C) 2018-2022 Intel Corporation
710 static int nl80211_channel2freq(int channel
, const char *band
, bool ax
)
715 if (!band
|| band
[0] != 'a')
719 else if (channel
< 14)
720 return (channel
* 5) + 2407;
722 else if (strcmp(band
, "ad") == 0)
725 return 56160 + 2160 * channel
;
732 return (channel
* 5) + 5950;
736 if (channel
>= 182 && channel
<= 196)
737 return (channel
* 5) + 4000;
739 return (channel
* 5) + 5000;
745 static uint8_t nl80211_freq2band(int freq
)
747 if (freq
>= 2412 && freq
<= 2484)
748 return IWINFO_BAND_24
;
749 else if (freq
>= 5160 && freq
<= 5885)
750 return IWINFO_BAND_5
;
751 else if (freq
>= 5925 && freq
<= 7125)
752 return IWINFO_BAND_6
;
753 else if (freq
>= 58320 && freq
<= 69120)
754 return IWINFO_BAND_60
;
759 static int nl80211_phyname_cb(struct nl_msg
*msg
, void *arg
)
762 struct nlattr
**attr
= nl80211_parse(msg
);
764 if (attr
[NL80211_ATTR_WIPHY_NAME
])
765 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
766 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
773 static char * nl80211_ifname2phy(const char *ifname
)
775 static char phy
[32] = { 0 };
777 memset(phy
, 0, sizeof(phy
));
779 nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
780 nl80211_phyname_cb
, phy
);
782 return phy
[0] ? phy
: NULL
;
785 static char * nl80211_phyidx2name(unsigned int idx
)
787 struct nl80211_msg_conveyor
*cv
;
788 static char phy
[32] = { 0 };
790 if (nl80211_init() < 0)
793 cv
= nl80211_new(nls
->nl80211
, NL80211_CMD_GET_WIPHY
, 0);
797 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, idx
);
799 memset(phy
, 0, sizeof(phy
));
800 nl80211_send(cv
, nl80211_phyname_cb
, phy
);
802 return phy
[0] ? phy
: NULL
;
808 static char * nl80211_phy2ifname(const char *ifname
)
810 int ifidx
= -1, cifidx
, lmode
= 1, clmode
, phyidx
;
812 static char nif
[IFNAMSIZ
] = { 0 };
816 /* Only accept phy name in the form of phy%d or radio%d */
820 phyidx
= nl80211_phy_idx_from_phy(ifname
);
822 phyidx
= nl80211_phy_idx_from_uci(ifname
);;
826 memset(nif
, 0, sizeof(nif
));
828 if ((d
= opendir("/sys/class/net")) != NULL
)
830 while ((e
= readdir(d
)) != NULL
)
832 snprintf(buffer
, sizeof(buffer
),
833 "/sys/class/net/%s/phy80211/index", e
->d_name
);
834 if (nl80211_readint(buffer
) != phyidx
)
837 snprintf(buffer
, sizeof(buffer
),
838 "/sys/class/net/%s/ifindex", e
->d_name
);
839 cifidx
= nl80211_readint(buffer
);
844 snprintf(buffer
, sizeof(buffer
),
845 "/sys/class/net/%s/link_mode", e
->d_name
);
846 clmode
= nl80211_readint(buffer
);
848 /* prefer non-supplicant-based devices */
849 if ((ifidx
< 0) || (cifidx
< ifidx
) || ((lmode
== 1) && (clmode
!= 1)))
853 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
860 return nif
[0] ? nif
: NULL
;
863 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
866 struct nlattr
**tb
= nl80211_parse(msg
);
867 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
868 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
869 IWINFO_OPMODE_ADHOC
, /* IBSS */
870 IWINFO_OPMODE_CLIENT
, /* managed */
871 IWINFO_OPMODE_MASTER
, /* AP */
872 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
873 IWINFO_OPMODE_WDS
, /* WDS */
874 IWINFO_OPMODE_MONITOR
, /* monitor */
875 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
876 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
877 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
880 if (tb
[NL80211_ATTR_IFTYPE
])
881 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
887 static int nl80211_get_mode(const char *ifname
, int *buf
)
891 *buf
= IWINFO_OPMODE_UNKNOWN
;
893 res
= nl80211_phy2ifname(ifname
);
895 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
896 nl80211_get_mode_cb
, buf
);
898 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
901 static int __nl80211_hostapd_query(const char *ifname
, ...)
904 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
905 int len
, mode
, found
= 0, match
= 1;
908 if (nl80211_get_mode(ifname
, &mode
))
911 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
914 phy
= nl80211_ifname2phy(ifname
);
919 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
920 fp
= fopen(buf
, "r");
925 va_start(ap
, ifname
);
927 /* clear all destination buffers */
930 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
932 dest
= va_arg(ap_cur
, char *);
933 len
= va_arg(ap_cur
, int);
935 memset(dest
, 0, len
);
940 /* iterate applicable lines and copy found values into dest buffers */
941 while (fgets(buf
, sizeof(buf
), fp
))
943 key
= strtok(buf
, " =\t\n");
944 val
= strtok(NULL
, "\n");
946 if (!key
|| !val
|| !*key
|| *key
== '#')
949 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
950 match
= !strcmp(ifname
, val
);
957 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
959 dest
= va_arg(ap_cur
, char *);
960 len
= va_arg(ap_cur
, int);
962 if (!strcmp(search
, key
))
964 strncpy(dest
, val
, len
- 1);
980 #define nl80211_hostapd_query(ifname, ...) \
981 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
984 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
987 struct timeval tv
= { 0, 256000 };
992 memset(buf
, 0, blen
);
994 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
997 if (!FD_ISSET(sock
, &rfds
))
1000 return recv(sock
, buf
, blen
- 1, 0);
1003 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
1005 struct sockaddr_un remote
= { 0 };
1006 size_t remote_length
, local_length
;
1008 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
1012 remote
.sun_family
= AF_UNIX
;
1013 remote_length
= sizeof(remote
.sun_family
) +
1014 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
1016 /* Set client socket file permissions so that bind() creates the client
1017 * socket with these permissions and there is no need to try to change
1018 * them with chmod() after bind() which would have potential issues with
1019 * race conditions. These permissions are needed to make sure the server
1020 * side (wpa_supplicant or hostapd) can reply to the control interface
1023 * The lchown() calls below after bind() are also part of the needed
1024 * operations to allow the response to go through. Those are using the
1025 * no-deference-symlinks version to avoid races. */
1026 fchmod(sock
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1028 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
1034 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
1040 local
->sun_family
= AF_UNIX
;
1041 local_length
= sizeof(local
->sun_family
) +
1042 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
1044 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
1050 /* Set group even if we do not have privileges to change owner */
1051 lchown(local
->sun_path
, -1, 101);
1052 lchown(local
->sun_path
, 101, 101);
1057 static int __nl80211_wpactl_query(const char *ifname
, ...)
1060 struct sockaddr_un local
= { 0 };
1061 int len
, mode
, found
= 0, sock
= -1;
1062 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
1064 if (nl80211_get_mode(ifname
, &mode
))
1067 if (mode
!= IWINFO_OPMODE_CLIENT
&&
1068 mode
!= IWINFO_OPMODE_ADHOC
&&
1069 mode
!= IWINFO_OPMODE_MESHPOINT
)
1072 sock
= nl80211_wpactl_connect(ifname
, &local
);
1077 va_start(ap
, ifname
);
1079 /* clear all destination buffers */
1080 va_copy(ap_cur
, ap
);
1082 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
1084 dest
= va_arg(ap_cur
, char *);
1085 len
= va_arg(ap_cur
, int);
1087 memset(dest
, 0, len
);
1092 send(sock
, "STATUS", 6, 0);
1096 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
1102 for (line
= strtok_r(buf
, "\n", &pos
);
1104 line
= strtok_r(NULL
, "\n", &pos
))
1106 key
= strtok(line
, "=");
1107 val
= strtok(NULL
, "\n");
1112 va_copy(ap_cur
, ap
);
1114 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
1116 dest
= va_arg(ap_cur
, char *);
1117 len
= va_arg(ap_cur
, int);
1119 if (!strcmp(search
, key
))
1121 strncpy(dest
, val
, len
- 1);
1136 unlink(local
.sun_path
);
1141 #define nl80211_wpactl_query(ifname, ...) \
1142 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
1145 static char * nl80211_ifadd(const char *ifname
)
1147 char path
[PATH_MAX
];
1148 static char nif
[IFNAMSIZ
] = { 0 };
1149 struct nl80211_msg_conveyor
*req
;
1152 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
1155 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
1157 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
1158 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
1160 nl80211_send(req
, NULL
, NULL
);
1162 snprintf(path
, sizeof(path
) - 1,
1163 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
1165 if ((sysfs
= fopen(path
, "w")) != NULL
)
1167 fwrite("0\n", 1, 2, sysfs
);
1180 static void nl80211_ifdel(const char *ifname
)
1182 struct nl80211_msg_conveyor
*req
;
1184 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
1187 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
1189 nl80211_send(req
, NULL
, NULL
);
1197 static void nl80211_hostapd_hup(const char *ifname
)
1201 char *phy
= nl80211_ifname2phy(ifname
);
1205 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
1206 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
1208 if (read(fd
, buf
, sizeof(buf
)) > 0)
1220 static int nl80211_probe(const char *ifname
)
1222 return !!nl80211_ifname2phy(ifname
);
1225 struct nl80211_ssid_bssid
{
1226 unsigned char *ssid
;
1227 unsigned char bssid
[7];
1230 static int nl80211_get_macaddr_cb(struct nl_msg
*msg
, void *arg
)
1232 struct nl80211_ssid_bssid
*sb
= arg
;
1233 struct nlattr
**tb
= nl80211_parse(msg
);
1235 if (tb
[NL80211_ATTR_MAC
]) {
1237 memcpy(sb
->bssid
+ 1, nla_data(tb
[NL80211_ATTR_MAC
]),
1238 sizeof(sb
->bssid
) - 1);
1244 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
1248 struct nl80211_ssid_bssid
*sb
= arg
;
1249 struct nlattr
**tb
= nl80211_parse(msg
);
1250 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1252 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1253 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
1254 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1257 if (!tb
[NL80211_ATTR_BSS
] ||
1258 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1260 !bss
[NL80211_BSS_BSSID
] ||
1261 !bss
[NL80211_BSS_STATUS
] ||
1262 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
1267 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1269 case NL80211_BSS_STATUS_ASSOCIATED
:
1270 case NL80211_BSS_STATUS_AUTHENTICATED
:
1271 case NL80211_BSS_STATUS_IBSS_JOINED
:
1275 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1276 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1278 while (ielen
>= 2 && ielen
>= ie
[1])
1282 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1293 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1302 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1305 struct nl80211_ssid_bssid sb
= { .ssid
= (unsigned char *)buf
};
1307 /* try to find ssid from scan dump results */
1308 res
= nl80211_phy2ifname(ifname
);
1311 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1312 nl80211_get_ssid_bssid_cb
, &sb
);
1314 /* failed, try to find from hostapd info */
1315 if (sb
.ssid
[0] == 0)
1316 nl80211_hostapd_query(ifname
, "ssid", sb
.ssid
,
1317 IWINFO_ESSID_MAX_SIZE
+ 1);
1319 /* failed, try to obtain Mesh ID */
1320 if (sb
.ssid
[0] == 0)
1321 iwinfo_ubus_query(res
? res
: ifname
, "mesh_id",
1322 buf
, IWINFO_ESSID_MAX_SIZE
+ 1);
1324 return (sb
.ssid
[0] == 0) ? -1 : 0;
1327 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1329 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1330 struct nl80211_ssid_bssid sb
= { };
1332 res
= nl80211_phy2ifname(ifname
);
1334 /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
1335 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1336 nl80211_get_macaddr_cb
, &sb
);
1338 /* failed, try to find bssid from scan dump results */
1339 if (sb
.bssid
[0] == 0)
1340 nl80211_request(res
? res
: ifname
,
1341 NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1342 nl80211_get_ssid_bssid_cb
, &sb
);
1344 /* failed, try to find mac from hostapd info */
1345 if ((sb
.bssid
[0] == 0) &&
1346 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1349 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1350 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1351 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1352 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1353 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1354 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1359 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1360 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1361 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1370 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1373 struct nlattr
**attr
= nl80211_parse(msg
);
1374 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1376 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1377 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1378 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1381 if (attr
[NL80211_ATTR_BSS
] &&
1382 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1383 attr
[NL80211_ATTR_BSS
], bss_policy
))
1385 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1386 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1392 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1395 struct nlattr
**tb
= nl80211_parse(msg
);
1397 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1398 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1403 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1405 char *res
, channel
[4] = { 0 }, hwmode
[3] = { 0 }, ax
[2] = { 0 };
1407 /* try to find frequency from interface info */
1408 res
= nl80211_phy2ifname(ifname
);
1411 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1412 nl80211_get_frequency_info_cb
, buf
);
1414 /* failed, try to find frequency from hostapd info */
1416 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1417 "channel", channel
, sizeof(channel
),
1418 "ieee80211ax", ax
, sizeof(ax
)) >= 2)
1420 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
, ax
[0] == '1');
1423 /* failed, try to find frequency from scan results */
1426 res
= nl80211_phy2ifname(ifname
);
1428 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
1429 nl80211_get_frequency_scan_cb
, buf
);
1432 return (*buf
== 0) ? -1 : 0;
1435 static int nl80211_get_center_freq1_cb(struct nl_msg
*msg
, void *arg
)
1438 struct nlattr
**tb
= nl80211_parse(msg
);
1440 if (tb
[NL80211_ATTR_CENTER_FREQ1
])
1441 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ1
]);
1446 static int nl80211_get_center_freq1(const char *ifname
, int *buf
)
1450 /* try to find frequency from interface info */
1451 res
= nl80211_phy2ifname(ifname
);
1454 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1455 nl80211_get_center_freq1_cb
, buf
);
1457 return (*buf
== 0) ? -1 : 0;
1460 static int nl80211_get_center_freq2_cb(struct nl_msg
*msg
, void *arg
)
1463 struct nlattr
**tb
= nl80211_parse(msg
);
1465 if (tb
[NL80211_ATTR_CENTER_FREQ2
])
1466 *freq
= nla_get_u32(tb
[NL80211_ATTR_CENTER_FREQ2
]);
1471 static int nl80211_get_center_freq2(const char *ifname
, int *buf
)
1475 /* try to find frequency from interface info */
1476 res
= nl80211_phy2ifname(ifname
);
1479 nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1480 nl80211_get_center_freq2_cb
, buf
);
1482 return (*buf
== 0) ? -1 : 0;
1485 static int nl80211_get_channel(const char *ifname
, int *buf
)
1487 if (!nl80211_get_frequency(ifname
, buf
))
1489 *buf
= nl80211_freq2channel(*buf
);
1496 static int nl80211_get_center_chan1(const char *ifname
, int *buf
)
1498 if (!nl80211_get_center_freq1(ifname
, buf
))
1500 *buf
= nl80211_freq2channel(*buf
);
1507 static int nl80211_get_center_chan2(const char *ifname
, int *buf
)
1509 if (!nl80211_get_center_freq2(ifname
, buf
))
1511 *buf
= nl80211_freq2channel(*buf
);
1518 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1521 struct nlattr
**tb
= nl80211_parse(msg
);
1523 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1524 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1529 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1533 res
= nl80211_phy2ifname(ifname
);
1536 if (nl80211_request(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0,
1537 nl80211_get_txpower_cb
, buf
))
1544 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1548 struct nl80211_rssi_rate
*rr
= arg
;
1549 struct nlattr
**attr
= nl80211_parse(msg
);
1550 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1551 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1553 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1554 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1555 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1556 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1557 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1558 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1559 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1560 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1561 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1562 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1563 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1566 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1567 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1568 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1569 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1570 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1573 if (attr
[NL80211_ATTR_STA_INFO
])
1575 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1576 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1578 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1580 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1581 rr
->rssi
= (rr
->rssi
* rr
->rssi_samples
+ dbm
) / (rr
->rssi_samples
+ 1);
1585 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1587 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1588 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1591 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1593 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1594 rr
->rate
= (rr
->rate
* rr
->rate_samples
+ mbit
) / (rr
->rate_samples
+ 1);
1605 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1610 memset(r
, 0, sizeof(*r
));
1612 if ((d
= opendir("/sys/class/net")) != NULL
)
1614 while ((de
= readdir(d
)) != NULL
)
1616 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1617 (!de
->d_name
[strlen(ifname
)] ||
1618 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1620 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
1621 NLM_F_DUMP
, nl80211_fill_signal_cb
, r
);
1629 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1631 struct nl80211_rssi_rate rr
;
1633 nl80211_fill_signal(ifname
, &rr
);
1635 if (rr
.rate_samples
)
1637 *buf
= (rr
.rate
* 100);
1644 static int nl80211_get_signal(const char *ifname
, int *buf
)
1646 struct nl80211_rssi_rate rr
;
1648 nl80211_fill_signal(ifname
, &rr
);
1650 if (rr
.rssi_samples
)
1659 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1661 int8_t *noise
= arg
;
1662 struct nlattr
**tb
= nl80211_parse(msg
);
1663 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1665 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1666 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1667 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1670 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1673 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1674 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1677 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1680 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1681 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1687 static int nl80211_get_noise(const char *ifname
, int *buf
)
1691 if (nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
,
1692 nl80211_get_noise_cb
, &noise
))
1703 static int nl80211_get_quality(const char *ifname
, int *buf
)
1707 if (!nl80211_get_signal(ifname
, &signal
))
1709 /* A positive signal level is usually just a quality
1710 * value, pass through as-is */
1716 /* The cfg80211 wext compat layer assumes a signal range
1717 * of -110 dBm to -40 dBm, the quality value is derived
1718 * by adding 110 to the signal level */
1723 else if (signal
> -40)
1726 *buf
= (signal
+ 110);
1735 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1737 /* The cfg80211 wext compat layer assumes a maximum
1744 static int nl80211_check_wepkey(const char *key
)
1748 switch (strlen(key
))
1752 return IWINFO_CIPHER_WEP40
;
1756 return IWINFO_CIPHER_WEP104
;
1767 } wpa_key_mgmt_strings
[] = {
1768 { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x
},
1769 { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x
},
1770 { "EAP-SUITE-B", 4, IWINFO_KMGMT_8021x
},
1771 { "EAP-SHA384", 4, IWINFO_KMGMT_8021x
},
1772 { "EAP-SHA256", 0, IWINFO_KMGMT_8021x
},
1773 { "PSK-SHA256", 0, IWINFO_KMGMT_PSK
},
1774 { "NONE", 0, IWINFO_KMGMT_NONE
},
1775 { "None", 0, IWINFO_KMGMT_NONE
},
1776 { "PSK", 0, IWINFO_KMGMT_PSK
},
1777 { "EAP", 0, IWINFO_KMGMT_8021x
},
1778 { "SAE", 4, IWINFO_KMGMT_SAE
},
1779 { "OWE", 4, IWINFO_KMGMT_OWE
}
1782 static void parse_wpa_suites(const char *str
, int defversion
,
1783 uint8_t *versions
, uint8_t *suites
)
1787 const char *p
, *q
, *m
, *sep
= " \t\n,-+/";
1793 for (i
= 0; i
< ARRAY_SIZE(wpa_key_mgmt_strings
); i
++)
1795 m
= wpa_key_mgmt_strings
[i
].match
;
1798 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1800 if (wpa_key_mgmt_strings
[i
].version
!= 0)
1801 version
= wpa_key_mgmt_strings
[i
].version
;
1803 version
= defversion
;
1805 *versions
|= version
;
1806 *suites
|= wpa_key_mgmt_strings
[i
].suite
;
1814 q
+= strcspn(q
, sep
);
1816 p
= q
+ strspn(q
, sep
);
1823 } wpa_cipher_strings
[] = {
1824 { "WEP-104", IWINFO_CIPHER_WEP104
},
1825 { "WEP-40", IWINFO_CIPHER_WEP40
},
1826 { "NONE", IWINFO_CIPHER_NONE
},
1827 { "TKIP", IWINFO_CIPHER_TKIP
},
1828 { "CCMP-256",IWINFO_CIPHER_CCMP256
},
1829 { "CCMP", IWINFO_CIPHER_CCMP
},
1830 { "GCMP-256",IWINFO_CIPHER_GCMP256
},
1831 { "GCMP", IWINFO_CIPHER_GCMP
}
1834 static void parse_wpa_ciphers(const char *str
, uint16_t *ciphers
)
1838 const char *m
, *p
, *q
, *sep
= " \t\n,-+/";
1844 for (i
= 0; i
< ARRAY_SIZE(wpa_cipher_strings
); i
++)
1846 m
= wpa_cipher_strings
[i
].match
;
1849 if (!strncmp(q
, m
, l
) && (!q
[l
] || strchr(sep
, q
[l
])))
1851 *ciphers
|= wpa_cipher_strings
[i
].cipher
;
1859 q
+= strcspn(q
, sep
);
1861 p
= q
+ strspn(q
, sep
);
1865 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1869 uint8_t wpa_version
= 0;
1870 char wpa
[2], wpa_key_mgmt
[64], wpa_pairwise
[16], wpa_groupwise
[16];
1871 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1874 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1876 /* WPA supplicant */
1877 if (nl80211_wpactl_query(ifname
,
1878 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1879 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1880 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1881 "mode", mode
, sizeof(mode
)))
1884 if (!strcmp(wpa_key_mgmt
, "NONE"))
1886 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1887 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1889 if (c
->pair_ciphers
!= 0 && c
->pair_ciphers
!= IWINFO_CIPHER_NONE
) {
1891 c
->auth_suites
= IWINFO_KMGMT_NONE
;
1892 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1895 c
->pair_ciphers
= 0;
1896 c
->group_ciphers
= 0;
1901 else if (!strcmp(mode
, "mesh") && !strcmp(wpa_key_mgmt
, "UNKNOWN"))
1905 c
->auth_suites
= IWINFO_KMGMT_SAE
;
1906 c
->pair_ciphers
= IWINFO_CIPHER_CCMP
;
1907 c
->group_ciphers
= IWINFO_CIPHER_CCMP
;
1913 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1914 parse_wpa_ciphers(wpa_groupwise
, &c
->group_ciphers
);
1918 if (!strncmp(p
, "WPA2-", 5) || !strncmp(p
, "WPA2/", 5))
1923 else if (!strncmp(p
, "WPA-", 4))
1929 parse_wpa_suites(p
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
1931 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1938 else if (nl80211_hostapd_query(ifname
,
1939 "wpa", wpa
, sizeof(wpa
),
1940 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1941 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1942 "auth_algs", auth_algs
, sizeof(auth_algs
),
1943 "wep_key0", wep_key0
, sizeof(wep_key0
),
1944 "wep_key1", wep_key1
, sizeof(wep_key1
),
1945 "wep_key2", wep_key2
, sizeof(wep_key2
),
1946 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1950 if (wpa_key_mgmt
[0])
1952 for (p
= strtok(wpa_key_mgmt
, " \t"); p
!= NULL
; p
= strtok(NULL
, " \t"))
1954 if (!strncmp(p
, "WPA-", 4))
1957 if (!strncmp(p
, "FT-", 3))
1960 parse_wpa_suites(p
, atoi(wpa
), &c
->wpa_version
, &c
->auth_suites
);
1963 c
->enabled
= c
->wpa_version
? 1 : 0;
1966 if (wpa_pairwise
[0])
1967 parse_wpa_ciphers(wpa_pairwise
, &c
->pair_ciphers
);
1971 switch (atoi(auth_algs
))
1974 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1978 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1982 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1983 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1987 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1988 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1989 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1990 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1992 c
->enabled
= (c
->auth_algs
&& c
->pair_ciphers
) ? 1 : 0;
1995 c
->group_ciphers
= c
->pair_ciphers
;
2000 /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
2001 else if (!nl80211_get_mode(ifname
, &opmode
) &&
2002 (opmode
== IWINFO_OPMODE_ADHOC
||
2003 opmode
== IWINFO_OPMODE_MESHPOINT
))
2014 static int nl80211_get_phyname(const char *ifname
, char *buf
)
2018 name
= nl80211_ifname2phy(ifname
);
2025 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
2027 name
= nl80211_ifname2phy(name
);
2031 strcpy(buf
, ifname
);
2040 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
2041 struct iwinfo_rate_entry
*re
)
2043 if (ri
[NL80211_RATE_INFO_BITRATE32
])
2044 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
2045 else if (ri
[NL80211_RATE_INFO_BITRATE
])
2046 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
2048 if (ri
[NL80211_RATE_INFO_HE_MCS
])
2051 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_MCS
]);
2053 if (ri
[NL80211_RATE_INFO_HE_NSS
])
2054 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_NSS
]);
2055 if (ri
[NL80211_RATE_INFO_HE_GI
])
2056 re
->he_gi
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_GI
]);
2057 if (ri
[NL80211_RATE_INFO_HE_DCM
])
2058 re
->he_dcm
= nla_get_u8(ri
[NL80211_RATE_INFO_HE_DCM
]);
2060 else if (ri
[NL80211_RATE_INFO_VHT_MCS
])
2063 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
2065 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
2066 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
2068 else if (ri
[NL80211_RATE_INFO_MCS
])
2071 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
2074 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
2076 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
2078 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
2080 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
2082 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
2083 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
2088 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
2089 re
->is_short_gi
= 1;
2091 re
->is_40mhz
= (re
->mhz
== 40);
2094 static int nl80211_get_survey_cb(struct nl_msg
*msg
, void *arg
)
2096 struct nl80211_array_buf
*arr
= arg
;
2097 struct iwinfo_survey_entry
*e
= arr
->buf
;
2098 struct nlattr
**attr
= nl80211_parse(msg
);
2099 struct nlattr
*sinfo
[NL80211_SURVEY_INFO_MAX
+ 1];
2102 static struct nla_policy survey_policy
[NL80211_SURVEY_INFO_MAX
+ 1] = {
2103 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
2104 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
2105 [NL80211_SURVEY_INFO_TIME
] = { .type
= NLA_U64
},
2106 [NL80211_SURVEY_INFO_TIME_BUSY
] = { .type
= NLA_U64
},
2107 [NL80211_SURVEY_INFO_TIME_EXT_BUSY
] = { .type
= NLA_U64
},
2108 [NL80211_SURVEY_INFO_TIME_RX
] = { .type
= NLA_U64
},
2109 [NL80211_SURVEY_INFO_TIME_TX
] = { .type
= NLA_U64
},
2112 rc
= nla_parse_nested(sinfo
, NL80211_SURVEY_INFO_MAX
,
2113 attr
[NL80211_ATTR_SURVEY_INFO
],
2118 /* advance to end of array */
2120 memset(e
, 0, sizeof(*e
));
2122 if (sinfo
[NL80211_SURVEY_INFO_FREQUENCY
])
2123 e
->mhz
= nla_get_u32(sinfo
[NL80211_SURVEY_INFO_FREQUENCY
]);
2125 if (sinfo
[NL80211_SURVEY_INFO_NOISE
])
2126 e
->noise
= nla_get_u8(sinfo
[NL80211_SURVEY_INFO_NOISE
]);
2128 if (sinfo
[NL80211_SURVEY_INFO_TIME
])
2129 e
->active_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME
]);
2131 if (sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
])
2132 e
->busy_time
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_BUSY
]);
2134 if (sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
])
2135 e
->busy_time_ext
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_EXT_BUSY
]);
2137 if (sinfo
[NL80211_SURVEY_INFO_TIME_RX
])
2138 e
->rxtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_RX
]);
2140 if (sinfo
[NL80211_SURVEY_INFO_TIME_TX
])
2141 e
->txtime
= nla_get_u64(sinfo
[NL80211_SURVEY_INFO_TIME_TX
]);
2148 static void plink_state_to_str(char *dst
, unsigned state
)
2151 case NL80211_PLINK_LISTEN
:
2152 strcpy(dst
, "LISTEN");
2154 case NL80211_PLINK_OPN_SNT
:
2155 strcpy(dst
, "OPN_SNT");
2157 case NL80211_PLINK_OPN_RCVD
:
2158 strcpy(dst
, "OPN_RCVD");
2160 case NL80211_PLINK_CNF_RCVD
:
2161 strcpy(dst
, "CNF_RCVD");
2163 case NL80211_PLINK_ESTAB
:
2164 strcpy(dst
, "ESTAB");
2166 case NL80211_PLINK_HOLDING
:
2167 strcpy(dst
, "HOLDING");
2169 case NL80211_PLINK_BLOCKED
:
2170 strcpy(dst
, "BLOCKED");
2173 strcpy(dst
, "UNKNOWN");
2178 static void power_mode_to_str(char *dst
, struct nlattr
*a
)
2180 enum nl80211_mesh_power_mode pm
= nla_get_u32(a
);
2183 case NL80211_MESH_POWER_ACTIVE
:
2184 strcpy(dst
, "ACTIVE");
2186 case NL80211_MESH_POWER_LIGHT_SLEEP
:
2187 strcpy(dst
, "LIGHT SLEEP");
2189 case NL80211_MESH_POWER_DEEP_SLEEP
:
2190 strcpy(dst
, "DEEP SLEEP");
2193 strcpy(dst
, "UNKNOWN");
2198 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
2200 struct nl80211_array_buf
*arr
= arg
;
2201 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
2202 struct nlattr
**attr
= nl80211_parse(msg
);
2203 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
2204 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
2205 struct nl80211_sta_flag_update
*sta_flags
;
2207 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
2208 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
2209 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
2210 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
2211 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
2212 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
2213 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
2214 [NL80211_STA_INFO_SIGNAL_AVG
] = { .type
= NLA_U8
},
2215 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
2216 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
2217 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
2218 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
2219 [NL80211_STA_INFO_CONNECTED_TIME
]= { .type
= NLA_U32
},
2220 [NL80211_STA_INFO_RX_DROP_MISC
] = { .type
= NLA_U64
},
2221 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
2222 [NL80211_STA_INFO_STA_FLAGS
] =
2223 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
2224 [NL80211_STA_INFO_EXPECTED_THROUGHPUT
] = { .type
= NLA_U32
},
2226 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
2227 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
2228 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
2229 [NL80211_STA_INFO_LOCAL_PM
] = { .type
= NLA_U32
},
2230 [NL80211_STA_INFO_PEER_PM
] = { .type
= NLA_U32
},
2231 [NL80211_STA_INFO_NONPEER_PM
] = { .type
= NLA_U32
},
2234 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
2235 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
2236 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
2237 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
2238 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
2241 /* advance to end of array */
2243 memset(e
, 0, sizeof(*e
));
2245 if (attr
[NL80211_ATTR_MAC
])
2246 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
2248 if (attr
[NL80211_ATTR_STA_INFO
] &&
2249 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
2250 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
2252 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
2253 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
2255 if (sinfo
[NL80211_STA_INFO_SIGNAL_AVG
])
2256 e
->signal_avg
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL_AVG
]);
2258 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
2259 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
2261 if (sinfo
[NL80211_STA_INFO_CONNECTED_TIME
])
2262 e
->connected_time
= nla_get_u32(sinfo
[NL80211_STA_INFO_CONNECTED_TIME
]);
2264 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
2265 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
2267 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
2268 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
2270 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
2271 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2272 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
2273 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
2275 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
2276 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
2277 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
2278 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
2280 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
2281 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
2283 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
2284 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
2286 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
2287 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
2289 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
2290 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
2292 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
2293 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
2295 if (sinfo
[NL80211_STA_INFO_RX_DROP_MISC
])
2296 e
->rx_drop_misc
= nla_get_u64(sinfo
[NL80211_STA_INFO_RX_DROP_MISC
]);
2298 if (sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
])
2299 e
->thr
= nla_get_u32(sinfo
[NL80211_STA_INFO_EXPECTED_THROUGHPUT
]);
2302 if (sinfo
[NL80211_STA_INFO_LLID
])
2303 e
->llid
= nla_get_u16(sinfo
[NL80211_STA_INFO_LLID
]);
2305 if (sinfo
[NL80211_STA_INFO_PLID
])
2306 e
->plid
= nla_get_u16(sinfo
[NL80211_STA_INFO_PLID
]);
2308 if (sinfo
[NL80211_STA_INFO_PLINK_STATE
])
2309 plink_state_to_str(e
->plink_state
,
2310 nla_get_u8(sinfo
[NL80211_STA_INFO_PLINK_STATE
]));
2312 if (sinfo
[NL80211_STA_INFO_LOCAL_PM
])
2313 power_mode_to_str(e
->local_ps
, sinfo
[NL80211_STA_INFO_LOCAL_PM
]);
2314 if (sinfo
[NL80211_STA_INFO_PEER_PM
])
2315 power_mode_to_str(e
->peer_ps
, sinfo
[NL80211_STA_INFO_PEER_PM
]);
2316 if (sinfo
[NL80211_STA_INFO_NONPEER_PM
])
2317 power_mode_to_str(e
->nonpeer_ps
, sinfo
[NL80211_STA_INFO_NONPEER_PM
]);
2320 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
2322 sta_flags
= (struct nl80211_sta_flag_update
*)
2323 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
2325 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
2326 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
2327 e
->is_authorized
= 1;
2329 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
2330 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
2331 e
->is_authenticated
= 1;
2333 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
2334 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
2335 e
->is_preamble_short
= 1;
2337 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
2338 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
2341 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
2342 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
2345 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
2346 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
2351 e
->noise
= 0; /* filled in by caller */
2357 static int nl80211_get_survey(const char *ifname
, char *buf
, int *len
)
2359 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2362 rc
= nl80211_request(ifname
, NL80211_CMD_GET_SURVEY
,
2363 NLM_F_DUMP
, nl80211_get_survey_cb
, &arr
);
2365 *len
= (arr
.count
* sizeof(struct iwinfo_survey_entry
));
2372 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
2377 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2378 struct iwinfo_assoclist_entry
*e
;
2380 if ((d
= opendir("/sys/class/net")) != NULL
)
2382 while ((de
= readdir(d
)) != NULL
)
2384 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
2385 (!de
->d_name
[strlen(ifname
)] ||
2386 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
2388 nl80211_request(de
->d_name
, NL80211_CMD_GET_STATION
,
2389 NLM_F_DUMP
, nl80211_get_assoclist_cb
, &arr
);
2395 if (!nl80211_get_noise(ifname
, &noise
))
2396 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
2399 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
2406 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
2409 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
2411 struct nlattr
**attr
= nl80211_parse(msg
);
2412 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2413 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2414 struct nlattr
*band
, *freq
;
2416 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2417 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2418 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2419 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2420 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2421 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2422 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2425 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
2428 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2430 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
2431 nla_len(band
), NULL
);
2433 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2435 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2436 nla_data(freq
), nla_len(freq
), freq_policy
);
2438 ch_cmp
= nl80211_freq2channel(nla_get_u32(
2439 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
2441 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
2442 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
2444 *dbm_max
= (int)(0.01 * nla_get_u32(
2445 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
2455 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
2458 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
2459 struct iwinfo_txpwrlist_entry entry
;
2461 if (nl80211_get_channel(ifname
, &ch_cur
))
2464 /* initialize the value pointer with channel for callback */
2467 err
= nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
2468 nl80211_get_txpwrlist_cb
, &dbm_max
);
2472 for (dbm_cur
= 0, dbm_cnt
= 0;
2474 dbm_cur
++, dbm_cnt
++)
2476 entry
.dbm
= dbm_cur
;
2477 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
2479 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2482 entry
.dbm
= dbm_max
;
2483 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
2485 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
2488 *len
= dbm_cnt
* sizeof(entry
);
2495 static void nl80211_get_scancrypto(char *spec
, struct iwinfo_crypto_entry
*c
)
2497 int wpa_version
= 0;
2498 char *p
, *q
, *proto
, *suites
;
2502 for (p
= strtok_r(spec
, "[]", &q
); p
; p
= strtok_r(NULL
, "[]", &q
)) {
2503 if (!strcmp(p
, "WEP")) {
2505 c
->auth_suites
= IWINFO_KMGMT_NONE
;
2506 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2507 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2511 proto
= strtok(p
, "-");
2512 suites
= strtok(NULL
, "]");
2514 if (!proto
|| !suites
)
2517 if (!strcmp(proto
, "WPA2") || !strcmp(proto
, "RSN"))
2519 else if (!strcmp(proto
, "WPA"))
2526 parse_wpa_suites(suites
, wpa_version
, &c
->wpa_version
, &c
->auth_suites
);
2527 parse_wpa_ciphers(suites
, &c
->pair_ciphers
);
2532 struct nl80211_scanlist
{
2533 struct iwinfo_scanlist_entry
*e
;
2538 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
2539 struct iwinfo_scanlist_entry
*e
)
2541 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2542 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
2543 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
2546 while (ielen
>= 2 && ielen
>= ie
[1])
2551 case 114: /* Mesh ID */
2552 if (e
->ssid
[0] == 0) {
2553 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
2554 memcpy(e
->ssid
, ie
+ 2, len
);
2560 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2561 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2564 case 221: /* Vendor */
2565 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2566 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2567 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2569 case 61: /* HT operation */
2571 e
->ht_chan_info
.primary_chan
= ie
[2];
2572 e
->ht_chan_info
.secondary_chan_off
= ie
[3] & 0x3;
2573 e
->ht_chan_info
.chan_width
= (ie
[4] & 0x4)>>2;
2576 case 192: /* VHT operation */
2578 e
->vht_chan_info
.chan_width
= ie
[2];
2579 e
->vht_chan_info
.center_chan_1
= ie
[3];
2580 e
->vht_chan_info
.center_chan_2
= ie
[4];
2590 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2595 struct nl80211_scanlist
*sl
= arg
;
2596 struct nlattr
**tb
= nl80211_parse(msg
);
2597 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2599 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2600 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2601 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2602 [NL80211_BSS_BSSID
] = { 0 },
2603 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2604 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2605 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2606 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2607 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2608 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2609 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2610 [NL80211_BSS_BEACON_IES
] = { 0 },
2613 if (!tb
[NL80211_ATTR_BSS
] ||
2614 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2616 !bss
[NL80211_BSS_BSSID
])
2621 if (bss
[NL80211_BSS_CAPABILITY
])
2622 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2626 memset(sl
->e
, 0, sizeof(*sl
->e
));
2627 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2630 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2631 else if (caps
& (1<<0))
2632 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2634 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2637 sl
->e
->crypto
.enabled
= 1;
2639 if (bss
[NL80211_BSS_FREQUENCY
])
2641 sl
->e
->mhz
= nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]);
2642 sl
->e
->band
= nl80211_freq2band(sl
->e
->mhz
);
2643 sl
->e
->channel
= nl80211_freq2channel(sl
->e
->mhz
);
2646 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2647 nl80211_get_scanlist_ie(bss
, sl
->e
);
2649 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2652 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2654 rssi
= sl
->e
->signal
- 0x100;
2658 else if (rssi
> -40)
2661 sl
->e
->quality
= (rssi
+ 110);
2662 sl
->e
->quality_max
= 70;
2665 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2667 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2668 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2677 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2679 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2681 if (nl80211_request(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0, NULL
, NULL
))
2684 if (nl80211_wait("nl80211", "scan",
2685 NL80211_CMD_NEW_SCAN_RESULTS
, NL80211_CMD_SCAN_ABORTED
))
2688 if (nl80211_request(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
,
2689 nl80211_get_scanlist_cb
, &sl
))
2692 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2700 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2703 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2704 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2710 if (len
+ 1 >= outlen
)
2720 out
[len
++] = '\n'; in
++;
2724 out
[len
++] = '\r'; in
++;
2728 out
[len
++] = '\t'; in
++;
2732 out
[len
++] = '\033'; in
++;
2736 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2737 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2759 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2761 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2762 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2763 struct sockaddr_un local
= { 0 };
2764 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2766 sock
= nl80211_wpactl_connect(ifname
, &local
);
2771 send(sock
, "ATTACH", 6, 0);
2772 send(sock
, "SCAN", 4, 0);
2775 * wait for scan results:
2776 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2777 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2778 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2779 * time to process the results, so try 72 + 2 + 1 times.
2781 for (tries
= 0; tries
< 75; tries
++)
2783 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2786 /* got an event notification */
2787 if (reply
[0] == '<')
2789 /* scan results are ready */
2790 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2792 /* send "SCAN_RESULTS" command */
2793 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2797 /* is another unrelated event, retry */
2801 /* scanning already in progress, keep awaiting results */
2802 else if (!strcmp(reply
, "FAIL-BUSY\n"))
2807 /* another failure, abort */
2808 else if (!strncmp(reply
, "FAIL-", 5))
2814 /* receive and parse scan results if the wait above didn't time out */
2815 while (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2817 /* received an event notification, receive again */
2818 if (reply
[0] == '<')
2821 nl80211_get_quality_max(ifname
, &qmax
);
2823 for (line
= strtok_r(reply
, "\n", &pos
);
2825 line
= strtok_r(NULL
, "\n", &pos
))
2827 /* skip header line */
2834 bssid
= strtok(line
, "\t");
2835 freq
= strtok(NULL
, "\t");
2836 signal
= strtok(NULL
, "\t");
2837 flags
= strtok(NULL
, "\t");
2838 ssid
= strtok(NULL
, "\n");
2840 if (!bssid
|| !freq
|| !signal
|| !flags
)
2844 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2845 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2846 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2847 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2848 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2849 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2853 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2858 if (strstr(flags
, "[MESH]"))
2859 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2860 else if (strstr(flags
, "[IBSS]"))
2861 e
->mode
= IWINFO_OPMODE_ADHOC
;
2863 e
->mode
= IWINFO_OPMODE_MASTER
;
2866 e
->mhz
= atoi(freq
);
2867 e
->band
= nl80211_freq2band(e
->mhz
);
2868 e
->channel
= nl80211_freq2channel(e
->mhz
);
2871 rssi
= atoi(signal
);
2877 /* The cfg80211 wext compat layer assumes a signal range
2878 * of -110 dBm to -40 dBm, the quality value is derived
2879 * by adding 110 to the signal level */
2882 else if (rssi
> -40)
2885 e
->quality
= (rssi
+ 110);
2893 e
->quality_max
= qmax
;
2896 nl80211_get_scancrypto(flags
, &e
->crypto
);
2902 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2907 unlink(local
.sun_path
);
2909 return (count
>= 0) ? 0 : -1;
2912 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2919 /* Got a radioX pseudo interface, find some interface on it or create one */
2920 if (!strncmp(ifname
, "radio", 5))
2922 /* Reuse existing interface */
2923 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2925 return nl80211_get_scanlist(res
, buf
, len
);
2928 /* Need to spawn a temporary iface for scanning */
2929 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2931 rv
= nl80211_get_scanlist(res
, buf
, len
);
2937 /* WPA supplicant */
2938 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2943 /* station / ad-hoc / monitor scan */
2944 else if (!nl80211_get_mode(ifname
, &mode
) &&
2945 (mode
== IWINFO_OPMODE_ADHOC
||
2946 mode
== IWINFO_OPMODE_MASTER
||
2947 mode
== IWINFO_OPMODE_CLIENT
||
2948 mode
== IWINFO_OPMODE_MONITOR
) &&
2949 iwinfo_ifup(ifname
))
2951 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2957 /* Got a temp interface, don't create yet another one */
2958 if (!strncmp(ifname
, "tmp.", 4))
2960 if (!iwinfo_ifup(ifname
))
2963 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2964 iwinfo_ifdown(ifname
);
2968 /* Spawn a new scan interface */
2971 if (!(res
= nl80211_ifadd(ifname
)))
2976 /* if we can take the new interface up, the driver supports an
2977 * additional interface and there's no need to tear down the ap */
2978 if (iwinfo_ifup(res
))
2980 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2984 /* driver cannot create secondary interface, take down ap
2986 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2988 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2990 iwinfo_ifup(ifname
);
2991 nl80211_hostapd_hup(ifname
);
3004 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
3006 int bands_remain
, freqs_remain
;
3008 struct nl80211_array_buf
*arr
= arg
;
3009 struct iwinfo_freqlist_entry
*e
;
3011 struct nlattr
**attr
= nl80211_parse(msg
);
3012 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3013 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
3014 struct nlattr
*band
, *freq
;
3019 if (attr
[NL80211_ATTR_WIPHY_BANDS
]) {
3020 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3022 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3023 nla_data(band
), nla_len(band
), NULL
);
3025 if (bands
[NL80211_BAND_ATTR_FREQS
]) {
3026 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
3028 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
3029 nla_data(freq
), nla_len(freq
), NULL
);
3031 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
3032 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
3035 e
->band
= nl80211_get_band(band
->nla_type
);
3036 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
3037 e
->channel
= nl80211_freq2channel(e
->mhz
);
3039 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS
])
3040 e
->flags
|= IWINFO_FREQ_NO_HT40MINUS
;
3041 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS
])
3042 e
->flags
|= IWINFO_FREQ_NO_HT40PLUS
;
3043 if (freqs
[NL80211_FREQUENCY_ATTR_NO_80MHZ
])
3044 e
->flags
|= IWINFO_FREQ_NO_80MHZ
;
3045 if (freqs
[NL80211_FREQUENCY_ATTR_NO_160MHZ
])
3046 e
->flags
|= IWINFO_FREQ_NO_160MHZ
;
3047 if (freqs
[NL80211_FREQUENCY_ATTR_NO_20MHZ
])
3048 e
->flags
|= IWINFO_FREQ_NO_20MHZ
;
3049 if (freqs
[NL80211_FREQUENCY_ATTR_NO_10MHZ
])
3050 e
->flags
|= IWINFO_FREQ_NO_10MHZ
;
3051 if (freqs
[NL80211_FREQUENCY_ATTR_NO_HE
])
3052 e
->flags
|= IWINFO_FREQ_NO_HE
;
3053 if (freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
3054 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
])
3055 e
->flags
|= IWINFO_FREQ_NO_IR
;
3056 if (freqs
[NL80211_FREQUENCY_ATTR_INDOOR_ONLY
])
3057 e
->flags
|= IWINFO_FREQ_INDOOR_ONLY
;
3059 /* keep backwards compatibility */
3060 e
->restricted
= (e
->flags
& IWINFO_FREQ_NO_IR
) ? 1 : 0;
3072 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
3074 struct nl80211_msg_conveyor
*cv
;
3075 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
3076 uint32_t features
= nl80211_get_protocol_features(ifname
);
3079 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3080 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3084 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3085 if (nl80211_send(cv
, nl80211_get_freqlist_cb
, &arr
))
3088 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
3098 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
3101 struct nlattr
**attr
= nl80211_parse(msg
);
3103 if (attr
[NL80211_ATTR_REG_ALPHA2
])
3104 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
3111 static int nl80211_get_country(const char *ifname
, char *buf
)
3113 if (nl80211_request(ifname
, NL80211_CMD_GET_REG
, 0,
3114 nl80211_get_country_cb
, buf
))
3120 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
3123 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
3124 const struct iwinfo_iso3166_label
*l
;
3126 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
3128 e
->iso3166
= l
->iso3166
;
3129 e
->ccode
[0] = (l
->iso3166
/ 256);
3130 e
->ccode
[1] = (l
->iso3166
% 256);
3134 *len
= (count
* sizeof(struct iwinfo_country_entry
));
3139 struct nl80211_modes
3149 uint16_t he_phy_cap
[6];
3152 static void nl80211_eval_modelist(struct nl80211_modes
*m
)
3154 /* Treat any nonzero capability as 11n */
3157 m
->hw
|= IWINFO_80211_N
;
3158 m
->ht
|= IWINFO_HTMODE_HT20
;
3160 if (m
->nl_ht
& (1 << 1))
3161 m
->ht
|= IWINFO_HTMODE_HT40
;
3164 if (m
->he_phy_cap
[0] != 0) {
3165 m
->hw
|= IWINFO_80211_AX
;
3166 m
->ht
|= IWINFO_HTMODE_HE20
;
3168 if (m
->he_phy_cap
[0] & BIT(9))
3169 m
->ht
|= IWINFO_HTMODE_HE40
;
3170 if (m
->he_phy_cap
[0] & BIT(10))
3171 m
->ht
|= IWINFO_HTMODE_HE40
| IWINFO_HTMODE_HE80
;
3172 if (m
->he_phy_cap
[0] & BIT(11))
3173 m
->ht
|= IWINFO_HTMODE_HE160
;
3174 if (m
->he_phy_cap
[0] & BIT(12))
3175 m
->ht
|= IWINFO_HTMODE_HE160
| IWINFO_HTMODE_HE80_80
;
3178 if (m
->bands
& IWINFO_BAND_24
)
3180 m
->hw
|= IWINFO_80211_B
;
3181 m
->hw
|= IWINFO_80211_G
;
3184 if (m
->bands
& IWINFO_BAND_5
)
3186 /* Treat any nonzero capability as 11ac */
3189 m
->hw
|= IWINFO_80211_AC
;
3190 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
3192 switch ((m
->nl_vht
>> 2) & 3)
3195 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
3199 m
->ht
|= IWINFO_HTMODE_VHT160
;
3204 m
->hw
|= IWINFO_80211_A
;
3208 if (m
->bands
& IWINFO_BAND_60
)
3210 m
->hw
|= IWINFO_80211_AD
;
3215 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
3217 struct nl80211_modes
*m
= arg
;
3219 struct nlattr
**attr
= nl80211_parse(msg
);
3220 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
3221 struct nlattr
*band
;
3223 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
3225 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
3227 m
->bands
|= nl80211_get_band(band
->nla_type
);
3229 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
3230 nla_data(band
), nla_len(band
), NULL
);
3232 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
3233 m
->nl_ht
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
3235 if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
3236 m
->nl_vht
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
3238 if (bands
[NL80211_BAND_ATTR_IFTYPE_DATA
]) {
3239 struct nlattr
*tb
[NL80211_BAND_IFTYPE_ATTR_MAX
+ 1];
3240 struct nlattr
*nl_iftype
;
3244 nla_for_each_nested(nl_iftype
, bands
[NL80211_BAND_ATTR_IFTYPE_DATA
], rem_band
) {
3245 nla_parse(tb
, NL80211_BAND_IFTYPE_ATTR_MAX
,
3246 nla_data(nl_iftype
), nla_len(nl_iftype
), NULL
);
3247 if (tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]) {
3248 len
= nla_len(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]);
3250 if (len
> sizeof(m
->he_phy_cap
) - 1)
3251 len
= sizeof(m
->he_phy_cap
) - 1;
3252 memcpy(&((__u8
*)m
->he_phy_cap
)[1],
3253 nla_data(tb
[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY
]),
3266 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
3268 struct nl80211_msg_conveyor
*cv
;
3269 struct nl80211_modes m
= {};
3270 uint32_t features
= nl80211_get_protocol_features(ifname
);
3273 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3274 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3278 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3279 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3280 goto nla_put_failure
;
3282 nl80211_eval_modelist(&m
);
3299 static int nl80211_get_htmode_cb(struct nl_msg
*msg
, void *arg
)
3301 struct nlattr
**tb
= nl80211_parse(msg
);
3303 struct chan_info
*chn
= arg
;
3305 if ((cur
= tb
[NL80211_ATTR_CHANNEL_WIDTH
]))
3306 chn
->width
= nla_get_u32(cur
);
3308 if ((cur
= tb
[NL80211_ATTR_BSS_HT_OPMODE
]))
3309 chn
->mode
= nla_get_u32(cur
);
3314 static int nl80211_get_htmode(const char *ifname
, int *buf
)
3316 struct chan_info chn
= { 0 };
3317 char *res
, b
[2] = { 0 };
3321 res
= nl80211_phy2ifname(ifname
);
3324 err
= nl80211_request(res
? res
: ifname
,
3325 NL80211_CMD_GET_INTERFACE
, 0,
3326 nl80211_get_htmode_cb
, &chn
);
3330 if (nl80211_hostapd_query(res
? res
: ifname
, "ieee80211ax", b
, sizeof(b
)))
3332 else if (nl80211_wpactl_query(res
? res
: ifname
, "wifi_generation", b
, sizeof(b
)))
3335 switch (chn
.width
) {
3336 case NL80211_CHAN_WIDTH_20
:
3338 *buf
= IWINFO_HTMODE_HE20
;
3339 else if (chn
.mode
== -1)
3340 *buf
= IWINFO_HTMODE_VHT20
;
3342 *buf
= IWINFO_HTMODE_HT20
;
3344 case NL80211_CHAN_WIDTH_40
:
3346 *buf
= IWINFO_HTMODE_HE40
;
3347 else if (chn
.mode
== -1)
3348 *buf
= IWINFO_HTMODE_VHT40
;
3350 *buf
= IWINFO_HTMODE_HT40
;
3352 case NL80211_CHAN_WIDTH_80
:
3354 *buf
= IWINFO_HTMODE_HE80
;
3356 *buf
= IWINFO_HTMODE_VHT80
;
3358 case NL80211_CHAN_WIDTH_80P80
:
3360 *buf
= IWINFO_HTMODE_HE80_80
;
3362 *buf
= IWINFO_HTMODE_VHT80_80
;
3364 case NL80211_CHAN_WIDTH_160
:
3366 *buf
= IWINFO_HTMODE_HE160
;
3368 *buf
= IWINFO_HTMODE_VHT160
;
3370 case NL80211_CHAN_WIDTH_5
:
3371 case NL80211_CHAN_WIDTH_10
:
3372 case NL80211_CHAN_WIDTH_20_NOHT
:
3373 *buf
= IWINFO_HTMODE_NOHT
;
3382 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
3384 struct nl80211_msg_conveyor
*cv
;
3385 struct nl80211_modes m
= {};
3386 uint32_t features
= nl80211_get_protocol_features(ifname
);
3389 flags
= features
& NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP
? NLM_F_DUMP
: 0;
3390 cv
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, flags
);
3394 NLA_PUT_FLAG(cv
->msg
, NL80211_ATTR_SPLIT_WIPHY_DUMP
);
3395 if (nl80211_send(cv
, nl80211_get_modelist_cb
, &m
))
3396 goto nla_put_failure
;
3398 nl80211_eval_modelist(&m
);
3411 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
3413 struct nlattr
**attr
= nl80211_parse(msg
);
3414 struct nlattr
*comb
;
3416 int comb_rem
, limit_rem
, mode_rem
;
3419 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
3422 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
3424 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
3425 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
3426 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
3428 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
3429 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
3430 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
3431 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
3433 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
3434 struct nlattr
*limit
;
3436 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
3438 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
3441 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
3443 struct nlattr
*mode
;
3445 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
3447 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
3448 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
3451 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
3454 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
3455 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
3464 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
3466 if (nl80211_request(ifname
, NL80211_CMD_GET_WIPHY
, 0,
3467 nl80211_get_ifcomb_cb
, buf
))
3473 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id
*id
, const char *ifname
)
3475 char *phy
, path
[PATH_MAX
];
3477 /* Try to determine the phy name from the given interface */
3478 phy
= nl80211_ifname2phy(ifname
);
3480 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/of_node/compatible",
3481 phy
? "ieee80211" : "net", phy
? phy
: ifname
);
3483 if (nl80211_readstr(path
, id
->compatible
, sizeof(id
->compatible
)) <= 0)
3490 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
3492 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
3493 char *phy
, num
[8], path
[PATH_MAX
];
3496 struct { const char *path
; uint16_t *dest
; } lookup
[] = {
3497 { "vendor", &id
->vendor_id
},
3498 { "device", &id
->device_id
},
3499 { "subsystem_vendor", &id
->subsystem_vendor_id
},
3500 { "subsystem_device", &id
->subsystem_device_id
},
3501 { "../idVendor", &id
->subsystem_vendor_id
},
3502 { "../idProduct", &id
->subsystem_device_id
}
3505 memset(id
, 0, sizeof(*id
));
3507 /* Try to determine the phy name from the given interface */
3508 phy
= nl80211_ifname2phy(ifname
);
3510 for (i
= 0; i
< ARRAY_SIZE(lookup
); i
++)
3512 snprintf(path
, sizeof(path
), "/sys/class/%s/%s/device/%s",
3513 phy
? "ieee80211" : "net",
3514 phy
? phy
: ifname
, lookup
[i
].path
);
3516 if (nl80211_readstr(path
, num
, sizeof(num
)) > 0)
3517 *lookup
[i
].dest
= strtoul(num
, NULL
, 16);
3520 /* Failed to obtain hardware PCI/USB IDs... */
3521 if (id
->vendor_id
== 0 && id
->device_id
== 0 &&
3522 id
->subsystem_vendor_id
== 0 && id
->subsystem_device_id
== 0)
3523 /* ... first fallback to FDT ... */
3524 if (nl80211_hardware_id_from_fdt(id
, ifname
) == -1)
3525 /* ... then board config */
3526 return iwinfo_hardware_id_from_mtd(id
);
3531 static const struct iwinfo_hardware_entry
*
3532 nl80211_get_hardware_entry(const char *ifname
)
3534 struct iwinfo_hardware_id id
;
3536 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
3539 return iwinfo_hardware(&id
);
3542 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
3544 const struct iwinfo_hardware_entry
*hw
;
3546 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3547 sprintf(buf
, "Generic MAC80211");
3549 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
3554 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
3556 const struct iwinfo_hardware_entry
*hw
;
3558 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3561 *buf
= hw
->txpower_offset
;
3565 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
3567 const struct iwinfo_hardware_entry
*hw
;
3569 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
3572 *buf
= hw
->frequency_offset
;
3576 static int nl80211_lookup_phyname(const char *section
, char *buf
)
3581 if (!strncmp(section
, "path=", 5))
3582 idx
= nl80211_phy_idx_from_path(section
+ 5);
3583 else if (!strncmp(section
, "macaddr=", 8))
3584 idx
= nl80211_phy_idx_from_macaddr(section
+ 8);
3586 idx
= nl80211_phy_idx_from_uci(section
);
3591 name
= nl80211_phyidx2name(idx
);
3599 static int nl80211_phy_path(const char *phyname
, const char **path
)
3601 if (strchr(phyname
, '/'))
3604 *path
= nl80211_phy_path_str(phyname
);
3611 const struct iwinfo_ops nl80211_ops
= {
3613 .probe
= nl80211_probe
,
3614 .channel
= nl80211_get_channel
,
3615 .center_chan1
= nl80211_get_center_chan1
,
3616 .center_chan2
= nl80211_get_center_chan2
,
3617 .frequency
= nl80211_get_frequency
,
3618 .frequency_offset
= nl80211_get_frequency_offset
,
3619 .txpower
= nl80211_get_txpower
,
3620 .txpower_offset
= nl80211_get_txpower_offset
,
3621 .bitrate
= nl80211_get_bitrate
,
3622 .signal
= nl80211_get_signal
,
3623 .noise
= nl80211_get_noise
,
3624 .quality
= nl80211_get_quality
,
3625 .quality_max
= nl80211_get_quality_max
,
3626 .mbssid_support
= nl80211_get_mbssid_support
,
3627 .hwmodelist
= nl80211_get_hwmodelist
,
3628 .htmodelist
= nl80211_get_htmodelist
,
3629 .htmode
= nl80211_get_htmode
,
3630 .mode
= nl80211_get_mode
,
3631 .ssid
= nl80211_get_ssid
,
3632 .bssid
= nl80211_get_bssid
,
3633 .country
= nl80211_get_country
,
3634 .hardware_id
= nl80211_get_hardware_id
,
3635 .hardware_name
= nl80211_get_hardware_name
,
3636 .encryption
= nl80211_get_encryption
,
3637 .phyname
= nl80211_get_phyname
,
3638 .assoclist
= nl80211_get_assoclist
,
3639 .txpwrlist
= nl80211_get_txpwrlist
,
3640 .scanlist
= nl80211_get_scanlist
,
3641 .freqlist
= nl80211_get_freqlist
,
3642 .countrylist
= nl80211_get_countrylist
,
3643 .survey
= nl80211_get_survey
,
3644 .lookup_phy
= nl80211_lookup_phyname
,
3645 .phy_path
= nl80211_phy_path
,
3646 .close
= nl80211_close