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.
30 #include "iwinfo_nl80211.h"
32 #define min(x, y) ((x) < (y)) ? (x) : (y)
34 #define BIT(x) (1ULL<<(x))
36 static struct nl80211_state
*nls
= NULL
;
38 static void nl80211_close(void)
43 genl_family_put(nls
->nlctrl
);
46 genl_family_put(nls
->nl80211
);
49 nl_socket_free(nls
->nl_sock
);
52 nl_cache_free(nls
->nl_cache
);
59 static int nl80211_init(void)
65 nls
= malloc(sizeof(struct nl80211_state
));
71 memset(nls
, 0, sizeof(*nls
));
73 nls
->nl_sock
= nl_socket_alloc();
79 if (genl_connect(nls
->nl_sock
)) {
84 fd
= nl_socket_get_fd(nls
->nl_sock
);
85 if (fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
) < 0) {
90 if (genl_ctrl_alloc_cache(nls
->nl_sock
, &nls
->nl_cache
)) {
95 nls
->nl80211
= genl_ctrl_search_by_name(nls
->nl_cache
, "nl80211");
101 nls
->nlctrl
= genl_ctrl_search_by_name(nls
->nl_cache
, "nlctrl");
116 static int nl80211_readint(const char *path
)
122 if ((fd
= open(path
, O_RDONLY
)) > -1)
124 if (read(fd
, buffer
, sizeof(buffer
)) > 0)
133 static int nl80211_readstr(const char *path
, char *buffer
, int length
)
138 if ((fd
= open(path
, O_RDONLY
)) > -1)
140 if ((rv
= read(fd
, buffer
, length
- 1)) > 0)
142 if (buffer
[rv
- 1] == '\n')
155 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
156 struct nlmsgerr
*err
, void *arg
)
163 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
170 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
177 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
182 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
197 static struct nl80211_msg_conveyor
* nl80211_new(struct genl_family
*family
,
200 static struct nl80211_msg_conveyor cv
;
202 struct nl_msg
*req
= NULL
;
203 struct nl_cb
*cb
= NULL
;
209 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
213 genlmsg_put(req
, 0, 0, genl_family_get_id(family
), 0, flags
, cmd
, 0);
227 static struct nl80211_msg_conveyor
* nl80211_ctl(int cmd
, int flags
)
229 if (nl80211_init() < 0)
232 return nl80211_new(nls
->nlctrl
, cmd
, flags
);
235 static int nl80211_phy_idx_from_uci_path(struct uci_section
*s
)
242 opt
= uci_lookup_option_string(uci_ctx
, s
, "path");
246 snprintf(buf
, sizeof(buf
), "/sys/devices/%s/ieee80211/*/index", opt
); /**/
247 if (glob(buf
, 0, NULL
, &gl
))
251 idx
= nl80211_readint(gl
.gl_pathv
[0]);
258 static int nl80211_phy_idx_from_uci_macaddr(struct uci_section
*s
)
265 opt
= uci_lookup_option_string(uci_ctx
, s
, "macaddr");
269 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/*"); /**/
270 if (glob(buf
, 0, NULL
, &gl
))
273 for (i
= 0; i
< gl
.gl_pathc
; i
++)
275 snprintf(buf
, sizeof(buf
), "%s/macaddress", gl
.gl_pathv
[i
]);
276 if (nl80211_readstr(buf
, buf
, sizeof(buf
)) <= 0)
279 if (fnmatch(opt
, buf
, FNM_CASEFOLD
))
282 snprintf(buf
, sizeof(buf
), "%s/index", gl
.gl_pathv
[i
]);
283 if ((idx
= nl80211_readint(buf
)) > -1)
292 static int nl80211_phy_idx_from_uci_phy(struct uci_section
*s
)
297 opt
= uci_lookup_option_string(uci_ctx
, s
, "phy");
301 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", opt
);
302 return nl80211_readint(buf
);
305 static int nl80211_phy_idx_from_uci(const char *name
)
307 struct uci_section
*s
;
310 s
= iwinfo_uci_get_radio(name
, "mac80211");
314 idx
= nl80211_phy_idx_from_uci_path(s
);
317 idx
= nl80211_phy_idx_from_uci_macaddr(s
);
320 idx
= nl80211_phy_idx_from_uci_phy(s
);
327 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
,
330 int ifidx
= -1, phyidx
= -1;
331 struct nl80211_msg_conveyor
*cv
;
336 if (nl80211_init() < 0)
339 if (!strncmp(ifname
, "phy", 3))
340 phyidx
= atoi(&ifname
[3]);
341 else if (!strncmp(ifname
, "radio", 5))
342 phyidx
= nl80211_phy_idx_from_uci(ifname
);
343 else if (!strncmp(ifname
, "mon.", 4))
344 ifidx
= if_nametoindex(&ifname
[4]);
346 ifidx
= if_nametoindex(ifname
);
348 /* Valid ifidx must be greater than 0 */
349 if ((ifidx
<= 0) && (phyidx
< 0))
352 cv
= nl80211_new(nls
->nl80211
, cmd
, flags
);
357 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_IFINDEX
, ifidx
);
360 NLA_PUT_U32(cv
->msg
, NL80211_ATTR_WIPHY
, phyidx
);
369 static struct nl80211_msg_conveyor
* nl80211_send(
370 struct nl80211_msg_conveyor
*cv
,
371 int (*cb_func
)(struct nl_msg
*, void *), void *cb_arg
373 static struct nl80211_msg_conveyor rcv
;
377 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
379 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
381 if (nl_send_auto_complete(nls
->nl_sock
, cv
->msg
) < 0)
384 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
385 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
386 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
389 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
400 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
402 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
403 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
405 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
406 genlmsg_attrlen(gnlh
, 0), NULL
);
412 static int nl80211_subscribe_cb(struct nl_msg
*msg
, void *arg
)
414 struct nl80211_group_conveyor
*cv
= arg
;
416 struct nlattr
**attr
= nl80211_parse(msg
);
417 struct nlattr
*mgrpinfo
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
421 if (!attr
[CTRL_ATTR_MCAST_GROUPS
])
424 nla_for_each_nested(mgrp
, attr
[CTRL_ATTR_MCAST_GROUPS
], mgrpidx
)
426 nla_parse(mgrpinfo
, CTRL_ATTR_MCAST_GRP_MAX
,
427 nla_data(mgrp
), nla_len(mgrp
), NULL
);
429 if (mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
] &&
430 mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
] &&
431 !strncmp(nla_data(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
]),
432 cv
->name
, nla_len(mgrpinfo
[CTRL_ATTR_MCAST_GRP_NAME
])))
434 cv
->id
= nla_get_u32(mgrpinfo
[CTRL_ATTR_MCAST_GRP_ID
]);
442 static int nl80211_subscribe(const char *family
, const char *group
)
444 struct nl80211_group_conveyor cv
= { .name
= group
, .id
= -ENOENT
};
445 struct nl80211_msg_conveyor
*req
;
447 req
= nl80211_ctl(CTRL_CMD_GETFAMILY
, 0);
450 NLA_PUT_STRING(req
->msg
, CTRL_ATTR_FAMILY_NAME
, family
);
451 nl80211_send(req
, nl80211_subscribe_cb
, &cv
);
457 return nl_socket_add_membership(nls
->nl_sock
, cv
.id
);
461 static int nl80211_wait_cb(struct nl_msg
*msg
, void *arg
)
463 struct nl80211_event_conveyor
*cv
= arg
;
464 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
466 if (gnlh
->cmd
== cv
->wait
)
467 cv
->recv
= gnlh
->cmd
;
472 static int nl80211_wait_seq_check(struct nl_msg
*msg
, void *arg
)
477 static int nl80211_wait(const char *family
, const char *group
, int cmd
)
479 struct nl80211_event_conveyor cv
= { .wait
= cmd
};
482 if (nl80211_subscribe(family
, group
))
485 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
490 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, nl80211_wait_seq_check
, NULL
);
491 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_wait_cb
, &cv
);
494 nl_recvmsgs(nls
->nl_sock
, cb
);
502 static int nl80211_freq2channel(int freq
)
506 else if (freq
< 2484)
507 return (freq
- 2407) / 5;
508 else if (freq
>= 4910 && freq
<= 4980)
509 return (freq
- 4000) / 5;
511 return (freq
- 5000) / 5;
514 static int nl80211_channel2freq(int channel
, const char *band
)
516 if (!band
|| band
[0] != 'a')
520 else if (channel
< 14)
521 return (channel
* 5) + 2407;
525 if (channel
>= 182 && channel
<= 196)
526 return (channel
* 5) + 4000;
528 return (channel
* 5) + 5000;
534 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
537 struct nlattr
**attr
= nl80211_parse(msg
);
539 if (attr
[NL80211_ATTR_WIPHY_NAME
])
540 memcpy(buf
, nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]),
541 nla_len(attr
[NL80211_ATTR_WIPHY_NAME
]));
548 static char * nl80211_ifname2phy(const char *ifname
)
550 static char phy
[32] = { 0 };
551 struct nl80211_msg_conveyor
*req
;
553 memset(phy
, 0, sizeof(phy
));
555 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
558 nl80211_send(req
, nl80211_ifname2phy_cb
, phy
);
562 return phy
[0] ? phy
: NULL
;
565 static char * nl80211_phy2ifname(const char *ifname
)
567 int ifidx
= -1, cifidx
= -1, phyidx
= -1;
569 static char nif
[IFNAMSIZ
] = { 0 };
574 /* Only accept phy name of the form phy%d or radio%d */
577 else if (!strncmp(ifname
, "phy", 3))
578 phyidx
= atoi(&ifname
[3]);
579 else if (!strncmp(ifname
, "radio", 5))
580 phyidx
= nl80211_phy_idx_from_uci(ifname
);
584 memset(nif
, 0, sizeof(nif
));
588 if ((d
= opendir("/sys/class/net")) != NULL
)
590 while ((e
= readdir(d
)) != NULL
)
592 snprintf(buffer
, sizeof(buffer
),
593 "/sys/class/net/%s/phy80211/index", e
->d_name
);
595 if (nl80211_readint(buffer
) == phyidx
)
597 snprintf(buffer
, sizeof(buffer
),
598 "/sys/class/net/%s/ifindex", e
->d_name
);
600 if ((cifidx
= nl80211_readint(buffer
)) >= 0 &&
601 ((ifidx
< 0) || (cifidx
< ifidx
)))
604 strncpy(nif
, e
->d_name
, sizeof(nif
) - 1);
613 return nif
[0] ? nif
: NULL
;
616 static int nl80211_get_mode_cb(struct nl_msg
*msg
, void *arg
)
619 struct nlattr
**tb
= nl80211_parse(msg
);
620 const int ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
621 IWINFO_OPMODE_UNKNOWN
, /* unspecified */
622 IWINFO_OPMODE_ADHOC
, /* IBSS */
623 IWINFO_OPMODE_CLIENT
, /* managed */
624 IWINFO_OPMODE_MASTER
, /* AP */
625 IWINFO_OPMODE_AP_VLAN
, /* AP/VLAN */
626 IWINFO_OPMODE_WDS
, /* WDS */
627 IWINFO_OPMODE_MONITOR
, /* monitor */
628 IWINFO_OPMODE_MESHPOINT
, /* mesh point */
629 IWINFO_OPMODE_P2P_CLIENT
, /* P2P-client */
630 IWINFO_OPMODE_P2P_GO
, /* P2P-GO */
633 if (tb
[NL80211_ATTR_IFTYPE
])
634 *mode
= ifmodes
[nla_get_u32(tb
[NL80211_ATTR_IFTYPE
])];
640 static int nl80211_get_mode(const char *ifname
, int *buf
)
643 struct nl80211_msg_conveyor
*req
;
645 res
= nl80211_phy2ifname(ifname
);
646 req
= nl80211_msg(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0);
647 *buf
= IWINFO_OPMODE_UNKNOWN
;
651 nl80211_send(req
, nl80211_get_mode_cb
, buf
);
655 return (*buf
== IWINFO_OPMODE_UNKNOWN
) ? -1 : 0;
658 static int __nl80211_hostapd_query(const char *ifname
, ...)
661 char *phy
, *search
, *dest
, *key
, *val
, buf
[128];
662 int len
, mode
, found
= 0, match
= 1;
665 if (nl80211_get_mode(ifname
, &mode
))
668 if (mode
!= IWINFO_OPMODE_MASTER
&& mode
!= IWINFO_OPMODE_AP_VLAN
)
671 phy
= nl80211_ifname2phy(ifname
);
676 snprintf(buf
, sizeof(buf
), "/var/run/hostapd-%s.conf", phy
);
677 fp
= fopen(buf
, "r");
682 va_start(ap
, ifname
);
684 /* clear all destination buffers */
687 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
689 dest
= va_arg(ap_cur
, char *);
690 len
= va_arg(ap_cur
, int);
692 memset(dest
, 0, len
);
697 /* iterate applicable lines and copy found values into dest buffers */
698 while (fgets(buf
, sizeof(buf
), fp
))
700 key
= strtok(buf
, " =\t\n");
701 val
= strtok(NULL
, "\n");
703 if (!key
|| !val
|| !*key
|| *key
== '#')
706 if (!strcmp(key
, "interface") || !strcmp(key
, "bss"))
707 match
= !strcmp(ifname
, val
);
714 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
716 dest
= va_arg(ap_cur
, char *);
717 len
= va_arg(ap_cur
, int);
719 if (!strcmp(search
, key
))
721 strncpy(dest
, val
, len
- 1);
737 #define nl80211_hostapd_query(ifname, ...) \
738 __nl80211_hostapd_query(ifname, ##__VA_ARGS__, NULL)
741 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
744 struct timeval tv
= { 0, 256000 };
749 memset(buf
, 0, blen
);
751 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
754 if (!FD_ISSET(sock
, &rfds
))
757 return recv(sock
, buf
, blen
- 1, 0);
760 static int nl80211_wpactl_connect(const char *ifname
, struct sockaddr_un
*local
)
762 struct sockaddr_un remote
= { 0 };
763 size_t remote_length
, local_length
;
765 int sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
769 remote
.sun_family
= AF_UNIX
;
770 remote_length
= sizeof(remote
.sun_family
) +
771 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant-%s/%s",
774 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
780 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
782 remote_length
= sizeof(remote
.sun_family
) +
783 sprintf(remote
.sun_path
, "/var/run/wpa_supplicant/%s", ifname
);
785 if (connect(sock
, (struct sockaddr
*)&remote
, remote_length
))
792 local
->sun_family
= AF_UNIX
;
793 local_length
= sizeof(local
->sun_family
) +
794 sprintf(local
->sun_path
, "/var/run/iwinfo-%s-%d", ifname
, getpid());
796 if (bind(sock
, (struct sockaddr
*)local
, local_length
) < 0)
805 static int __nl80211_wpactl_query(const char *ifname
, ...)
808 struct sockaddr_un local
= { 0 };
809 int len
, mode
, found
= 0, sock
= -1;
810 char *search
, *dest
, *key
, *val
, *line
, *pos
, buf
[512];
812 if (nl80211_get_mode(ifname
, &mode
))
815 if (mode
!= IWINFO_OPMODE_CLIENT
&& mode
!= IWINFO_OPMODE_ADHOC
)
818 sock
= nl80211_wpactl_connect(ifname
, &local
);
823 va_start(ap
, ifname
);
825 /* clear all destination buffers */
828 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
830 dest
= va_arg(ap_cur
, char *);
831 len
= va_arg(ap_cur
, int);
833 memset(dest
, 0, len
);
838 send(sock
, "STATUS", 6, 0);
842 if (nl80211_wpactl_recv(sock
, buf
, sizeof(buf
)) <= 0)
848 for (line
= strtok_r(buf
, "\n", &pos
);
850 line
= strtok_r(NULL
, "\n", &pos
))
852 key
= strtok(line
, "=");
853 val
= strtok(NULL
, "\n");
860 while ((search
= va_arg(ap_cur
, char *)) != NULL
)
862 dest
= va_arg(ap_cur
, char *);
863 len
= va_arg(ap_cur
, int);
865 if (!strcmp(search
, key
))
867 strncpy(dest
, val
, len
- 1);
882 unlink(local
.sun_path
);
887 #define nl80211_wpactl_query(ifname, ...) \
888 __nl80211_wpactl_query(ifname, ##__VA_ARGS__, NULL)
891 static char * nl80211_ifadd(const char *ifname
)
893 char *rv
= NULL
, path
[PATH_MAX
];
894 static char nif
[IFNAMSIZ
] = { 0 };
895 struct nl80211_msg_conveyor
*req
;
898 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
901 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
903 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
904 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
906 nl80211_send(req
, NULL
, NULL
);
908 snprintf(path
, sizeof(path
) - 1,
909 "/proc/sys/net/ipv6/conf/%s/disable_ipv6", nif
);
911 if ((sysfs
= fopen(path
, "w")) != NULL
)
913 fwrite("0\n", 1, 2, sysfs
);
926 static void nl80211_ifdel(const char *ifname
)
928 struct nl80211_msg_conveyor
*req
;
930 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
933 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
935 nl80211_send(req
, NULL
, NULL
);
942 static void nl80211_hostapd_hup(const char *ifname
)
946 char *phy
= nl80211_ifname2phy(ifname
);
950 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
951 if ((fd
= open(buf
, O_RDONLY
)) >= 0)
953 if (read(fd
, buf
, sizeof(buf
)) > 0)
965 static int nl80211_probe(const char *ifname
)
967 return !!nl80211_ifname2phy(ifname
);
970 struct nl80211_ssid_bssid
{
972 unsigned char bssid
[7];
975 static int nl80211_get_ssid_bssid_cb(struct nl_msg
*msg
, void *arg
)
979 struct nl80211_ssid_bssid
*sb
= arg
;
980 struct nlattr
**tb
= nl80211_parse(msg
);
981 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
983 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
984 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
985 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
988 if (!tb
[NL80211_ATTR_BSS
] ||
989 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
991 !bss
[NL80211_BSS_BSSID
] ||
992 !bss
[NL80211_BSS_STATUS
] ||
993 !bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
998 switch (nla_get_u32(bss
[NL80211_BSS_STATUS
]))
1000 case NL80211_BSS_STATUS_ASSOCIATED
:
1001 case NL80211_BSS_STATUS_AUTHENTICATED
:
1002 case NL80211_BSS_STATUS_IBSS_JOINED
:
1006 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1007 ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1009 while (ielen
>= 2 && ielen
>= ie
[1])
1013 memcpy(sb
->ssid
, ie
+ 2, min(ie
[1], IWINFO_ESSID_MAX_SIZE
));
1024 memcpy(sb
->bssid
+ 1, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
1033 static int nl80211_get_ssid(const char *ifname
, char *buf
)
1036 struct nl80211_msg_conveyor
*req
;
1037 struct nl80211_ssid_bssid sb
;
1039 /* try to find ssid from scan dump results */
1040 res
= nl80211_phy2ifname(ifname
);
1041 req
= nl80211_msg(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
);
1043 sb
.ssid
= (unsigned char *)buf
;
1048 nl80211_send(req
, nl80211_get_ssid_bssid_cb
, &sb
);
1052 /* failed, try to find from hostapd info */
1054 nl80211_hostapd_query(ifname
, "ssid", buf
, IWINFO_ESSID_MAX_SIZE
+ 1);
1056 return (*buf
== 0) ? -1 : 0;
1059 static int nl80211_get_bssid(const char *ifname
, char *buf
)
1061 char *res
, bssid
[sizeof("FF:FF:FF:FF:FF:FF\0")];
1062 struct nl80211_msg_conveyor
*req
;
1063 struct nl80211_ssid_bssid sb
;
1065 /* try to find bssid from scan dump results */
1066 res
= nl80211_phy2ifname(ifname
);
1067 req
= nl80211_msg(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
);
1074 nl80211_send(req
, nl80211_get_ssid_bssid_cb
, &sb
);
1078 /* failed, try to find mac from hostapd info */
1079 if ((sb
.bssid
[0] == 0) &&
1080 nl80211_hostapd_query(ifname
, "bssid", bssid
, sizeof(bssid
)))
1083 sb
.bssid
[1] = strtol(&bssid
[0], NULL
, 16);
1084 sb
.bssid
[2] = strtol(&bssid
[3], NULL
, 16);
1085 sb
.bssid
[3] = strtol(&bssid
[6], NULL
, 16);
1086 sb
.bssid
[4] = strtol(&bssid
[9], NULL
, 16);
1087 sb
.bssid
[5] = strtol(&bssid
[12], NULL
, 16);
1088 sb
.bssid
[6] = strtol(&bssid
[15], NULL
, 16);
1093 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
1094 sb
.bssid
[1], sb
.bssid
[2], sb
.bssid
[3],
1095 sb
.bssid
[4], sb
.bssid
[5], sb
.bssid
[6]);
1104 static int nl80211_get_frequency_scan_cb(struct nl_msg
*msg
, void *arg
)
1107 struct nlattr
**attr
= nl80211_parse(msg
);
1108 struct nlattr
*binfo
[NL80211_BSS_MAX
+ 1];
1110 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1111 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1112 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1115 if (attr
[NL80211_ATTR_BSS
] &&
1116 !nla_parse_nested(binfo
, NL80211_BSS_MAX
,
1117 attr
[NL80211_ATTR_BSS
], bss_policy
))
1119 if (binfo
[NL80211_BSS_STATUS
] && binfo
[NL80211_BSS_FREQUENCY
])
1120 *freq
= nla_get_u32(binfo
[NL80211_BSS_FREQUENCY
]);
1126 static int nl80211_get_frequency_info_cb(struct nl_msg
*msg
, void *arg
)
1129 struct nlattr
**tb
= nl80211_parse(msg
);
1131 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
1132 *freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
1137 static int nl80211_get_frequency(const char *ifname
, int *buf
)
1139 char *res
, channel
[4], hwmode
[2];
1140 struct nl80211_msg_conveyor
*req
;
1142 /* try to find frequency from interface info */
1143 res
= nl80211_phy2ifname(ifname
);
1144 req
= nl80211_msg(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0);
1149 nl80211_send(req
, nl80211_get_frequency_info_cb
, buf
);
1153 /* failed, try to find frequency from hostapd info */
1155 nl80211_hostapd_query(ifname
, "hw_mode", hwmode
, sizeof(hwmode
),
1156 "channel", channel
, sizeof(channel
)) == 2)
1158 *buf
= nl80211_channel2freq(atoi(channel
), hwmode
);
1161 /* failed, try to find frequency from scan results */
1164 res
= nl80211_phy2ifname(ifname
);
1165 req
= nl80211_msg(res
? res
: ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
);
1169 nl80211_send(req
, nl80211_get_frequency_scan_cb
, buf
);
1174 return (*buf
== 0) ? -1 : 0;
1177 static int nl80211_get_channel(const char *ifname
, int *buf
)
1179 if (!nl80211_get_frequency(ifname
, buf
))
1181 *buf
= nl80211_freq2channel(*buf
);
1188 static int nl80211_get_txpower_cb(struct nl_msg
*msg
, void *arg
)
1191 struct nlattr
**tb
= nl80211_parse(msg
);
1193 if (tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
])
1194 *buf
= iwinfo_mbm2dbm(nla_get_u32(tb
[NL80211_ATTR_WIPHY_TX_POWER_LEVEL
]));
1199 static int nl80211_get_txpower(const char *ifname
, int *buf
)
1202 struct nl80211_msg_conveyor
*req
;
1204 res
= nl80211_phy2ifname(ifname
);
1205 req
= nl80211_msg(res
? res
: ifname
, NL80211_CMD_GET_INTERFACE
, 0);
1210 nl80211_send(req
, nl80211_get_txpower_cb
, buf
);
1220 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
1224 struct nl80211_rssi_rate
*rr
= arg
;
1225 struct nlattr
**attr
= nl80211_parse(msg
);
1226 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1227 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1229 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1230 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1231 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1232 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1233 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1234 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1235 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1236 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1237 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1238 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1239 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1242 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1243 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1244 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1245 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1246 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1249 if (attr
[NL80211_ATTR_STA_INFO
])
1251 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1252 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1254 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1256 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1257 rr
->rssi
= rr
->rssi
? (int8_t)((rr
->rssi
+ dbm
) / 2) : dbm
;
1260 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
1262 if (!nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1263 sinfo
[NL80211_STA_INFO_TX_BITRATE
],
1266 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
1268 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
1270 ? (int16_t)((rr
->rate
+ mbit
) / 2) : mbit
;
1280 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
1284 struct nl80211_msg_conveyor
*req
;
1289 if ((d
= opendir("/sys/class/net")) != NULL
)
1291 while ((de
= readdir(d
)) != NULL
)
1293 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1294 (!de
->d_name
[strlen(ifname
)] ||
1295 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1297 req
= nl80211_msg(de
->d_name
, NL80211_CMD_GET_STATION
,
1302 nl80211_send(req
, nl80211_fill_signal_cb
, r
);
1312 static int nl80211_get_bitrate(const char *ifname
, int *buf
)
1314 struct nl80211_rssi_rate rr
;
1316 nl80211_fill_signal(ifname
, &rr
);
1320 *buf
= (rr
.rate
* 100);
1327 static int nl80211_get_signal(const char *ifname
, int *buf
)
1329 struct nl80211_rssi_rate rr
;
1331 nl80211_fill_signal(ifname
, &rr
);
1342 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
1344 int8_t *noise
= arg
;
1345 struct nlattr
**tb
= nl80211_parse(msg
);
1346 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
1348 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
1349 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
1350 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
1353 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
1356 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
1357 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
1360 if (!si
[NL80211_SURVEY_INFO_NOISE
])
1363 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
1364 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
1370 static int nl80211_get_noise(const char *ifname
, int *buf
)
1373 struct nl80211_msg_conveyor
*req
;
1375 req
= nl80211_msg(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
);
1380 nl80211_send(req
, nl80211_get_noise_cb
, &noise
);
1393 static int nl80211_get_quality(const char *ifname
, int *buf
)
1397 if (!nl80211_get_signal(ifname
, &signal
))
1399 /* A positive signal level is usually just a quality
1400 * value, pass through as-is */
1406 /* The cfg80211 wext compat layer assumes a signal range
1407 * of -110 dBm to -40 dBm, the quality value is derived
1408 * by adding 110 to the signal level */
1413 else if (signal
> -40)
1416 *buf
= (signal
+ 110);
1425 static int nl80211_get_quality_max(const char *ifname
, int *buf
)
1427 /* The cfg80211 wext compat layer assumes a maximum
1434 static int nl80211_check_wepkey(const char *key
)
1438 switch (strlen(key
))
1442 return IWINFO_CIPHER_WEP40
;
1446 return IWINFO_CIPHER_WEP104
;
1453 static int nl80211_get_encryption(const char *ifname
, char *buf
)
1455 char wpa
[2], wpa_key_mgmt
[16], wpa_pairwise
[16], wpa_groupwise
[16];
1456 char auth_algs
[2], wep_key0
[27], wep_key1
[27], wep_key2
[27], wep_key3
[27];
1458 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
1460 /* WPA supplicant */
1461 if (nl80211_wpactl_query(ifname
,
1462 "pairwise_cipher", wpa_pairwise
, sizeof(wpa_pairwise
),
1463 "group_cipher", wpa_groupwise
, sizeof(wpa_groupwise
),
1464 "key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
)))
1467 if (!strcmp(wpa_key_mgmt
, "NONE"))
1469 if (strstr(wpa_pairwise
, "WEP-40"))
1470 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
1471 else if (strstr(wpa_pairwise
, "WEP-104"))
1472 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
1474 if (strstr(wpa_groupwise
, "WEP-40"))
1475 c
->group_ciphers
|= IWINFO_CIPHER_WEP40
;
1476 else if (strstr(wpa_groupwise
, "WEP-104"))
1477 c
->group_ciphers
|= IWINFO_CIPHER_WEP104
;
1479 c
->enabled
= !!(c
->pair_ciphers
| c
->group_ciphers
);
1480 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1481 c
->auth_algs
|= IWINFO_AUTH_OPEN
; /* XXX: assumption */
1485 else if (strstr(wpa_key_mgmt
, "WPA"))
1487 if (strstr(wpa_pairwise
, "TKIP"))
1488 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
1489 else if (strstr(wpa_pairwise
, "CCMP"))
1490 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
1491 else if (strstr(wpa_pairwise
, "NONE"))
1492 c
->pair_ciphers
|= IWINFO_CIPHER_NONE
;
1493 else if (strstr(wpa_pairwise
, "WEP-40"))
1494 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
1495 else if (strstr(wpa_pairwise
, "WEP-104"))
1496 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
1498 if (strstr(wpa_groupwise
, "TKIP"))
1499 c
->group_ciphers
|= IWINFO_CIPHER_TKIP
;
1500 else if (strstr(wpa_groupwise
, "CCMP"))
1501 c
->group_ciphers
|= IWINFO_CIPHER_CCMP
;
1502 else if (strstr(wpa_groupwise
, "NONE"))
1503 c
->group_ciphers
|= IWINFO_CIPHER_NONE
;
1504 else if (strstr(wpa_groupwise
, "WEP-40"))
1505 c
->group_ciphers
|= IWINFO_CIPHER_WEP40
;
1506 else if (strstr(wpa_groupwise
, "WEP-104"))
1507 c
->group_ciphers
|= IWINFO_CIPHER_WEP104
;
1509 if (strstr(wpa_key_mgmt
, "WPA2"))
1511 else if (strstr(wpa_key_mgmt
, "WPA"))
1514 if (strstr(wpa_key_mgmt
, "PSK"))
1515 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1516 else if (strstr(wpa_key_mgmt
, "EAP") ||
1517 strstr(wpa_key_mgmt
, "802.1X"))
1518 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
1519 else if (strstr(wpa_key_mgmt
, "NONE"))
1520 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1522 c
->enabled
= !!(c
->wpa_version
&& c
->auth_suites
);
1529 else if (nl80211_hostapd_query(ifname
,
1530 "wpa", wpa
, sizeof(wpa
),
1531 "wpa_key_mgmt", wpa_key_mgmt
, sizeof(wpa_key_mgmt
),
1532 "wpa_pairwise", wpa_pairwise
, sizeof(wpa_pairwise
),
1533 "auth_algs", auth_algs
, sizeof(auth_algs
),
1534 "wep_key0", wep_key0
, sizeof(wep_key0
),
1535 "wep_key1", wep_key1
, sizeof(wep_key1
),
1536 "wep_key2", wep_key2
, sizeof(wep_key2
),
1537 "wep_key3", wep_key3
, sizeof(wep_key3
)))
1539 c
->wpa_version
= wpa
[0] ? atoi(wpa
) : 0;
1541 if (wpa_key_mgmt
[0])
1543 if (strstr(wpa_key_mgmt
, "PSK"))
1544 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1546 if (strstr(wpa_key_mgmt
, "EAP"))
1547 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
1549 if (strstr(wpa_key_mgmt
, "NONE"))
1550 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1554 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1557 if (wpa_pairwise
[0])
1559 if (strstr(wpa_pairwise
, "TKIP"))
1560 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
1562 if (strstr(wpa_pairwise
, "CCMP"))
1563 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
1565 if (strstr(wpa_pairwise
, "NONE"))
1566 c
->pair_ciphers
|= IWINFO_CIPHER_NONE
;
1571 switch(atoi(auth_algs
))
1574 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1578 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1582 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1583 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1587 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key0
);
1588 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key1
);
1589 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key2
);
1590 c
->pair_ciphers
|= nl80211_check_wepkey(wep_key3
);
1593 c
->group_ciphers
= c
->pair_ciphers
;
1594 c
->enabled
= (c
->wpa_version
|| c
->pair_ciphers
) ? 1 : 0;
1602 static int nl80211_get_phyname(const char *ifname
, char *buf
)
1606 name
= nl80211_ifname2phy(ifname
);
1613 else if ((name
= nl80211_phy2ifname(ifname
)) != NULL
)
1615 name
= nl80211_ifname2phy(name
);
1619 strcpy(buf
, ifname
);
1628 static void nl80211_parse_rateinfo(struct nlattr
**ri
,
1629 struct iwinfo_rate_entry
*re
)
1631 if (ri
[NL80211_RATE_INFO_BITRATE32
])
1632 re
->rate
= nla_get_u32(ri
[NL80211_RATE_INFO_BITRATE32
]) * 100;
1633 else if (ri
[NL80211_RATE_INFO_BITRATE
])
1634 re
->rate
= nla_get_u16(ri
[NL80211_RATE_INFO_BITRATE
]) * 100;
1636 if (ri
[NL80211_RATE_INFO_VHT_MCS
])
1639 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_MCS
]);
1641 if (ri
[NL80211_RATE_INFO_VHT_NSS
])
1642 re
->nss
= nla_get_u8(ri
[NL80211_RATE_INFO_VHT_NSS
]);
1644 else if (ri
[NL80211_RATE_INFO_MCS
])
1647 re
->mcs
= nla_get_u8(ri
[NL80211_RATE_INFO_MCS
]);
1650 if (ri
[NL80211_RATE_INFO_5_MHZ_WIDTH
])
1652 else if (ri
[NL80211_RATE_INFO_10_MHZ_WIDTH
])
1654 else if (ri
[NL80211_RATE_INFO_40_MHZ_WIDTH
])
1656 else if (ri
[NL80211_RATE_INFO_80_MHZ_WIDTH
])
1658 else if (ri
[NL80211_RATE_INFO_80P80_MHZ_WIDTH
] ||
1659 ri
[NL80211_RATE_INFO_160_MHZ_WIDTH
])
1664 if (ri
[NL80211_RATE_INFO_SHORT_GI
])
1665 re
->is_short_gi
= 1;
1667 re
->is_40mhz
= (re
->mhz
== 40);
1670 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
1672 struct nl80211_array_buf
*arr
= arg
;
1673 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
1674 struct nlattr
**attr
= nl80211_parse(msg
);
1675 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1676 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
1677 struct nl80211_sta_flag_update
*sta_flags
;
1679 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1680 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1681 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1682 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1683 [NL80211_STA_INFO_RX_BITRATE
] = { .type
= NLA_NESTED
},
1684 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1685 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1686 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1687 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1688 [NL80211_STA_INFO_TX_RETRIES
] = { .type
= NLA_U32
},
1689 [NL80211_STA_INFO_TX_FAILED
] = { .type
= NLA_U32
},
1690 [NL80211_STA_INFO_T_OFFSET
] = { .type
= NLA_U64
},
1691 [NL80211_STA_INFO_STA_FLAGS
] =
1692 { .minlen
= sizeof(struct nl80211_sta_flag_update
) },
1695 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
1696 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
1697 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
1698 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
1699 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
1702 /* advance to end of array */
1704 memset(e
, 0, sizeof(*e
));
1706 if (attr
[NL80211_ATTR_MAC
])
1707 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
1709 if (attr
[NL80211_ATTR_STA_INFO
] &&
1710 !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1711 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1713 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1714 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1716 if (sinfo
[NL80211_STA_INFO_INACTIVE_TIME
])
1717 e
->inactive
= nla_get_u32(sinfo
[NL80211_STA_INFO_INACTIVE_TIME
]);
1719 if (sinfo
[NL80211_STA_INFO_RX_PACKETS
])
1720 e
->rx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_PACKETS
]);
1722 if (sinfo
[NL80211_STA_INFO_TX_PACKETS
])
1723 e
->tx_packets
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_PACKETS
]);
1725 if (sinfo
[NL80211_STA_INFO_RX_BITRATE
] &&
1726 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1727 sinfo
[NL80211_STA_INFO_RX_BITRATE
], rate_policy
))
1728 nl80211_parse_rateinfo(rinfo
, &e
->rx_rate
);
1730 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
] &&
1731 !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
1732 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
))
1733 nl80211_parse_rateinfo(rinfo
, &e
->tx_rate
);
1735 if (sinfo
[NL80211_STA_INFO_RX_BYTES
])
1736 e
->rx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_RX_BYTES
]);
1738 if (sinfo
[NL80211_STA_INFO_TX_BYTES
])
1739 e
->tx_bytes
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_BYTES
]);
1741 if (sinfo
[NL80211_STA_INFO_TX_RETRIES
])
1742 e
->tx_retries
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_RETRIES
]);
1744 if (sinfo
[NL80211_STA_INFO_TX_FAILED
])
1745 e
->tx_failed
= nla_get_u32(sinfo
[NL80211_STA_INFO_TX_FAILED
]);
1747 if (sinfo
[NL80211_STA_INFO_T_OFFSET
])
1748 e
->t_offset
= nla_get_u64(sinfo
[NL80211_STA_INFO_T_OFFSET
]);
1751 if (sinfo
[NL80211_STA_INFO_STA_FLAGS
])
1753 sta_flags
= (struct nl80211_sta_flag_update
*)
1754 nla_data(sinfo
[NL80211_STA_INFO_STA_FLAGS
]);
1756 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHORIZED
) &&
1757 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHORIZED
))
1758 e
->is_authorized
= 1;
1760 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_AUTHENTICATED
) &&
1761 sta_flags
->set
& BIT(NL80211_STA_FLAG_AUTHENTICATED
))
1762 e
->is_authenticated
= 1;
1764 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
) &&
1765 sta_flags
->set
& BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
))
1766 e
->is_preamble_short
= 1;
1768 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_WME
) &&
1769 sta_flags
->set
& BIT(NL80211_STA_FLAG_WME
))
1772 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_MFP
) &&
1773 sta_flags
->set
& BIT(NL80211_STA_FLAG_MFP
))
1776 if (sta_flags
->mask
& BIT(NL80211_STA_FLAG_TDLS_PEER
) &&
1777 sta_flags
->set
& BIT(NL80211_STA_FLAG_TDLS_PEER
))
1782 e
->noise
= 0; /* filled in by caller */
1788 static int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
1793 struct nl80211_msg_conveyor
*req
;
1794 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
1795 struct iwinfo_assoclist_entry
*e
;
1797 if ((d
= opendir("/sys/class/net")) != NULL
)
1799 while ((de
= readdir(d
)) != NULL
)
1801 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1802 (!de
->d_name
[strlen(ifname
)] ||
1803 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1805 req
= nl80211_msg(de
->d_name
, NL80211_CMD_GET_STATION
,
1810 nl80211_send(req
, nl80211_get_assoclist_cb
, &arr
);
1818 if (!nl80211_get_noise(ifname
, &noise
))
1819 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
1822 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
1829 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
1832 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
1834 struct nlattr
**attr
= nl80211_parse(msg
);
1835 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
1836 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1837 struct nlattr
*band
, *freq
;
1839 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
1840 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
1841 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
1842 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
1843 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
1844 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
1845 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
1848 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
1851 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
1853 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
1854 nla_len(band
), NULL
);
1856 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
1858 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
1859 nla_data(freq
), nla_len(freq
), freq_policy
);
1861 ch_cmp
= nl80211_freq2channel(nla_get_u32(
1862 freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
1864 if ((!ch_cur
|| (ch_cmp
== ch_cur
)) &&
1865 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
])
1867 *dbm_max
= (int)(0.01 * nla_get_u32(
1868 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
1878 static int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
1881 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
1882 struct nl80211_msg_conveyor
*req
;
1883 struct iwinfo_txpwrlist_entry entry
;
1885 if (nl80211_get_channel(ifname
, &ch_cur
))
1888 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
1891 /* initialize the value pointer with channel for callback */
1894 nl80211_send(req
, nl80211_get_txpwrlist_cb
, &dbm_max
);
1900 for (dbm_cur
= 0, dbm_cnt
= 0;
1902 dbm_cur
++, dbm_cnt
++)
1904 entry
.dbm
= dbm_cur
;
1905 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
1907 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
1910 entry
.dbm
= dbm_max
;
1911 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
1913 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
1916 *len
= dbm_cnt
* sizeof(entry
);
1923 static void nl80211_get_scancrypto(const char *spec
,
1924 struct iwinfo_crypto_entry
*c
)
1926 if (strstr(spec
, "WPA") || strstr(spec
, "WEP"))
1930 if (strstr(spec
, "WPA2-") && strstr(spec
, "WPA-"))
1933 else if (strstr(spec
, "WPA2"))
1936 else if (strstr(spec
, "WPA"))
1939 else if (strstr(spec
, "WEP"))
1940 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1943 if (strstr(spec
, "PSK"))
1944 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1946 if (strstr(spec
, "802.1X") || strstr(spec
, "EAP"))
1947 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
1949 if (strstr(spec
, "WPA-NONE"))
1950 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1953 if (strstr(spec
, "TKIP"))
1954 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
1956 if (strstr(spec
, "CCMP"))
1957 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
1959 if (strstr(spec
, "WEP-40"))
1960 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
1962 if (strstr(spec
, "WEP-104"))
1963 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
1965 c
->group_ciphers
= c
->pair_ciphers
;
1974 struct nl80211_scanlist
{
1975 struct iwinfo_scanlist_entry
*e
;
1980 static void nl80211_get_scanlist_ie(struct nlattr
**bss
,
1981 struct iwinfo_scanlist_entry
*e
)
1983 int ielen
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1984 unsigned char *ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1985 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
1988 while (ielen
>= 2 && ielen
>= ie
[1])
1993 len
= min(ie
[1], IWINFO_ESSID_MAX_SIZE
);
1994 memcpy(e
->ssid
, ie
+ 2, len
);
1999 iwinfo_parse_rsn(&e
->crypto
, ie
+ 2, ie
[1],
2000 IWINFO_CIPHER_CCMP
, IWINFO_KMGMT_8021x
);
2003 case 221: /* Vendor */
2004 if (ie
[1] >= 4 && !memcmp(ie
+ 2, ms_oui
, 3) && ie
[5] == 1)
2005 iwinfo_parse_rsn(&e
->crypto
, ie
+ 6, ie
[1] - 4,
2006 IWINFO_CIPHER_TKIP
, IWINFO_KMGMT_PSK
);
2015 static int nl80211_get_scanlist_cb(struct nl_msg
*msg
, void *arg
)
2020 struct nl80211_scanlist
*sl
= arg
;
2021 struct nlattr
**tb
= nl80211_parse(msg
);
2022 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
2024 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
2025 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
2026 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
2027 [NL80211_BSS_BSSID
] = { 0 },
2028 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
2029 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
2030 [NL80211_BSS_INFORMATION_ELEMENTS
] = { 0 },
2031 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
2032 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
2033 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
2034 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
2035 [NL80211_BSS_BEACON_IES
] = { 0 },
2038 if (!tb
[NL80211_ATTR_BSS
] ||
2039 nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
2041 !bss
[NL80211_BSS_BSSID
])
2046 if (bss
[NL80211_BSS_CAPABILITY
])
2047 caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
2051 memset(sl
->e
, 0, sizeof(*sl
->e
));
2052 memcpy(sl
->e
->mac
, nla_data(bss
[NL80211_BSS_BSSID
]), 6);
2055 sl
->e
->mode
= IWINFO_OPMODE_ADHOC
;
2056 else if (caps
& (1<<0))
2057 sl
->e
->mode
= IWINFO_OPMODE_MASTER
;
2059 sl
->e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2062 sl
->e
->crypto
.enabled
= 1;
2064 if (bss
[NL80211_BSS_FREQUENCY
])
2065 sl
->e
->channel
= nl80211_freq2channel(nla_get_u32(
2066 bss
[NL80211_BSS_FREQUENCY
]));
2068 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
])
2069 nl80211_get_scanlist_ie(bss
, sl
->e
);
2071 if (bss
[NL80211_BSS_SIGNAL_MBM
])
2074 (uint8_t)((int32_t)nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]) / 100);
2076 rssi
= sl
->e
->signal
- 0x100;
2080 else if (rssi
> -40)
2083 sl
->e
->quality
= (rssi
+ 110);
2084 sl
->e
->quality_max
= 70;
2087 if (sl
->e
->crypto
.enabled
&& !sl
->e
->crypto
.wpa_version
)
2089 sl
->e
->crypto
.auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
2090 sl
->e
->crypto
.pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
2099 static int nl80211_get_scanlist_nl(const char *ifname
, char *buf
, int *len
)
2101 struct nl80211_msg_conveyor
*req
;
2102 struct nl80211_scanlist sl
= { .e
= (struct iwinfo_scanlist_entry
*)buf
};
2104 req
= nl80211_msg(ifname
, NL80211_CMD_TRIGGER_SCAN
, 0);
2107 nl80211_send(req
, NULL
, NULL
);
2111 nl80211_wait("nl80211", "scan", NL80211_CMD_NEW_SCAN_RESULTS
);
2113 req
= nl80211_msg(ifname
, NL80211_CMD_GET_SCAN
, NLM_F_DUMP
);
2116 nl80211_send(req
, nl80211_get_scanlist_cb
, &sl
);
2120 *len
= sl
.len
* sizeof(struct iwinfo_scanlist_entry
);
2121 return *len
? 0 : -1;
2124 static int wpasupp_ssid_decode(const char *in
, char *out
, int outlen
)
2127 (((x) >= 'a') ? ((x) - 'a' + 10) : \
2128 (((x) >= 'A') ? ((x) - 'A' + 10) : ((x) - '0')))
2134 if (len
+ 1 >= outlen
)
2144 out
[len
++] = '\n'; in
++;
2148 out
[len
++] = '\r'; in
++;
2152 out
[len
++] = '\t'; in
++;
2156 out
[len
++] = '\033'; in
++;
2160 if (isxdigit(*(in
+1)) && isxdigit(*(in
+2)))
2161 out
[len
++] = hex(*(in
+1)) * 16 + hex(*(in
+2));
2183 static int nl80211_get_scanlist_wpactl(const char *ifname
, char *buf
, int *len
)
2185 int sock
, qmax
, rssi
, tries
, count
= -1, ready
= 0;
2186 char *pos
, *line
, *bssid
, *freq
, *signal
, *flags
, *ssid
, reply
[4096];
2187 struct sockaddr_un local
= { 0 };
2188 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
2190 sock
= nl80211_wpactl_connect(ifname
, &local
);
2195 send(sock
, "ATTACH", 6, 0);
2196 send(sock
, "SCAN", 4, 0);
2199 * wait for scan results:
2200 * nl80211_wpactl_recv() will use a timeout of 256ms and we need to scan
2201 * 72 channels at most. We'll also receive two "OK" messages acknowledging
2202 * the "ATTACH" and "SCAN" commands and the driver might need a bit extra
2203 * time to process the results, so try 72 + 2 + 1 times.
2205 for (tries
= 0; tries
< 75; tries
++)
2207 if (nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) <= 0)
2210 /* got an event notification */
2211 if (reply
[0] == '<')
2213 /* scan results are ready */
2214 if (strstr(reply
, "CTRL-EVENT-SCAN-RESULTS"))
2216 /* send "SCAN_RESULTS" command */
2217 ready
= (send(sock
, "SCAN_RESULTS", 12, 0) == 12);
2221 /* is another unrelated event, retry */
2226 /* receive and parse scan results if the wait above didn't time out */
2227 if (ready
&& nl80211_wpactl_recv(sock
, reply
, sizeof(reply
)) > 0)
2229 nl80211_get_quality_max(ifname
, &qmax
);
2231 for (line
= strtok_r(reply
, "\n", &pos
);
2233 line
= strtok_r(NULL
, "\n", &pos
))
2235 /* skip header line */
2242 bssid
= strtok(line
, "\t");
2243 freq
= strtok(NULL
, "\t");
2244 signal
= strtok(NULL
, "\t");
2245 flags
= strtok(NULL
, "\t");
2246 ssid
= strtok(NULL
, "\n");
2248 if (!bssid
|| !freq
|| !signal
|| !flags
|| !ssid
)
2252 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
2253 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
2254 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
2255 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
2256 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
2257 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
2260 wpasupp_ssid_decode(ssid
, e
->ssid
, sizeof(e
->ssid
));
2263 if (strstr(flags
, "[MESH]"))
2264 e
->mode
= IWINFO_OPMODE_MESHPOINT
;
2265 else if (strstr(flags
, "[IBSS]"))
2266 e
->mode
= IWINFO_OPMODE_ADHOC
;
2268 e
->mode
= IWINFO_OPMODE_MASTER
;
2271 e
->channel
= nl80211_freq2channel(atoi(freq
));
2274 rssi
= atoi(signal
);
2280 /* The cfg80211 wext compat layer assumes a signal range
2281 * of -110 dBm to -40 dBm, the quality value is derived
2282 * by adding 110 to the signal level */
2285 else if (rssi
> -40)
2288 e
->quality
= (rssi
+ 110);
2296 e
->quality_max
= qmax
;
2299 nl80211_get_scancrypto(flags
, &e
->crypto
);
2305 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
2309 unlink(local
.sun_path
);
2311 return (count
>= 0) ? 0 : -1;
2314 static int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
2321 /* Got a radioX pseudo interface, find some interface on it or create one */
2322 if (!strncmp(ifname
, "radio", 5))
2324 /* Reuse existing interface */
2325 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2327 return nl80211_get_scanlist(res
, buf
, len
);
2330 /* Need to spawn a temporary iface for scanning */
2331 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2333 rv
= nl80211_get_scanlist(res
, buf
, len
);
2339 /* WPA supplicant */
2340 if (!nl80211_get_scanlist_wpactl(ifname
, buf
, len
))
2345 /* station / ad-hoc / monitor scan */
2346 else if (!nl80211_get_mode(ifname
, &mode
) &&
2347 (mode
== IWINFO_OPMODE_ADHOC
||
2348 mode
== IWINFO_OPMODE_MASTER
||
2349 mode
== IWINFO_OPMODE_CLIENT
||
2350 mode
== IWINFO_OPMODE_MONITOR
) &&
2351 iwinfo_ifup(ifname
))
2353 return nl80211_get_scanlist_nl(ifname
, buf
, len
);
2359 /* Got a temp interface, don't create yet another one */
2360 if (!strncmp(ifname
, "tmp.", 4))
2362 if (!iwinfo_ifup(ifname
))
2365 rv
= nl80211_get_scanlist_nl(ifname
, buf
, len
);
2366 iwinfo_ifdown(ifname
);
2370 /* Spawn a new scan interface */
2373 if (!(res
= nl80211_ifadd(ifname
)))
2378 /* if we can take the new interface up, the driver supports an
2379 * additional interface and there's no need to tear down the ap */
2380 if (iwinfo_ifup(res
))
2382 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2386 /* driver cannot create secondary interface, take down ap
2388 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
2390 rv
= nl80211_get_scanlist_nl(res
, buf
, len
);
2392 iwinfo_ifup(ifname
);
2393 nl80211_hostapd_hup(ifname
);
2404 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
2406 int bands_remain
, freqs_remain
;
2408 struct nl80211_array_buf
*arr
= arg
;
2409 struct iwinfo_freqlist_entry
*e
= arr
->buf
;
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 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2418 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2419 nla_data(band
), nla_len(band
), NULL
);
2421 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
2423 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2424 nla_data(freq
), nla_len(freq
), NULL
);
2426 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
2427 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
])
2430 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
2431 e
->channel
= nl80211_freq2channel(e
->mhz
);
2434 freqs
[NL80211_FREQUENCY_ATTR_NO_IR
] &&
2435 !freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
2446 static int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
2448 struct nl80211_msg_conveyor
*req
;
2449 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
2451 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
2454 nl80211_send(req
, nl80211_get_freqlist_cb
, &arr
);
2460 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
2467 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
2470 struct nlattr
**attr
= nl80211_parse(msg
);
2472 if (attr
[NL80211_ATTR_REG_ALPHA2
])
2473 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
2480 static int nl80211_get_country(const char *ifname
, char *buf
)
2483 struct nl80211_msg_conveyor
*req
;
2485 req
= nl80211_msg(ifname
, NL80211_CMD_GET_REG
, 0);
2488 nl80211_send(req
, nl80211_get_country_cb
, buf
);
2498 static int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
2501 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
2502 const struct iwinfo_iso3166_label
*l
;
2504 for (l
= IWINFO_ISO3166_NAMES
, count
= 0; l
->iso3166
; l
++, e
++, count
++)
2506 e
->iso3166
= l
->iso3166
;
2507 e
->ccode
[0] = (l
->iso3166
/ 256);
2508 e
->ccode
[1] = (l
->iso3166
% 256);
2512 *len
= (count
* sizeof(struct iwinfo_country_entry
));
2517 struct nl80211_modes
2524 static int nl80211_get_modelist_cb(struct nl_msg
*msg
, void *arg
)
2526 struct nl80211_modes
*m
= arg
;
2527 int bands_remain
, freqs_remain
;
2529 uint32_t vht_caps
= 0;
2530 struct nlattr
**attr
= nl80211_parse(msg
);
2531 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
2532 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2533 struct nlattr
*band
, *freq
;
2535 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
2537 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
2539 nla_parse(bands
, NL80211_BAND_ATTR_MAX
,
2540 nla_data(band
), nla_len(band
), NULL
);
2542 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
2543 caps
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
2545 /* Treat any nonzero capability as 11n */
2548 m
->hw
|= IWINFO_80211_N
;
2549 m
->ht
|= IWINFO_HTMODE_HT20
;
2551 if (caps
& (1 << 1))
2552 m
->ht
|= IWINFO_HTMODE_HT40
;
2555 nla_for_each_nested(freq
, bands
[NL80211_BAND_ATTR_FREQS
],
2558 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
2559 nla_data(freq
), nla_len(freq
), NULL
);
2561 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
2564 if (nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]) < 2485)
2566 m
->hw
|= IWINFO_80211_B
;
2567 m
->hw
|= IWINFO_80211_G
;
2569 else if (bands
[NL80211_BAND_ATTR_VHT_CAPA
])
2571 vht_caps
= nla_get_u32(bands
[NL80211_BAND_ATTR_VHT_CAPA
]);
2573 /* Treat any nonzero capability as 11ac */
2576 m
->hw
|= IWINFO_80211_AC
;
2577 m
->ht
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
| IWINFO_HTMODE_VHT80
;
2579 switch ((vht_caps
>> 2) & 3)
2582 m
->ht
|= IWINFO_HTMODE_VHT80_80
;
2586 m
->ht
|= IWINFO_HTMODE_VHT160
;
2590 else if (!(m
->hw
& IWINFO_80211_AC
))
2592 m
->hw
|= IWINFO_80211_A
;
2603 static int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
2605 struct nl80211_msg_conveyor
*req
;
2606 struct nl80211_modes m
= { 0 };
2608 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
2611 nl80211_send(req
, nl80211_get_modelist_cb
, &m
);
2624 static int nl80211_get_htmodelist(const char *ifname
, int *buf
)
2626 struct nl80211_msg_conveyor
*req
;
2627 struct nl80211_modes m
= { 0 };
2629 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
2632 nl80211_send(req
, nl80211_get_modelist_cb
, &m
);
2646 static int nl80211_get_ifcomb_cb(struct nl_msg
*msg
, void *arg
)
2648 struct nlattr
**attr
= nl80211_parse(msg
);
2649 struct nlattr
*comb
;
2651 int comb_rem
, limit_rem
, mode_rem
;
2654 if (!attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
])
2657 nla_for_each_nested(comb
, attr
[NL80211_ATTR_INTERFACE_COMBINATIONS
], comb_rem
)
2659 static struct nla_policy iface_combination_policy
[NUM_NL80211_IFACE_COMB
] = {
2660 [NL80211_IFACE_COMB_LIMITS
] = { .type
= NLA_NESTED
},
2661 [NL80211_IFACE_COMB_MAXNUM
] = { .type
= NLA_U32
},
2663 struct nlattr
*tb_comb
[NUM_NL80211_IFACE_COMB
+1];
2664 static struct nla_policy iface_limit_policy
[NUM_NL80211_IFACE_LIMIT
] = {
2665 [NL80211_IFACE_LIMIT_TYPES
] = { .type
= NLA_NESTED
},
2666 [NL80211_IFACE_LIMIT_MAX
] = { .type
= NLA_U32
},
2668 struct nlattr
*tb_limit
[NUM_NL80211_IFACE_LIMIT
+1];
2669 struct nlattr
*limit
;
2671 nla_parse_nested(tb_comb
, NUM_NL80211_IFACE_COMB
, comb
, iface_combination_policy
);
2673 if (!tb_comb
[NL80211_IFACE_COMB_LIMITS
])
2676 nla_for_each_nested(limit
, tb_comb
[NL80211_IFACE_COMB_LIMITS
], limit_rem
)
2678 struct nlattr
*mode
;
2680 nla_parse_nested(tb_limit
, NUM_NL80211_IFACE_LIMIT
, limit
, iface_limit_policy
);
2682 if (!tb_limit
[NL80211_IFACE_LIMIT_TYPES
] ||
2683 !tb_limit
[NL80211_IFACE_LIMIT_MAX
])
2686 if (nla_get_u32(tb_limit
[NL80211_IFACE_LIMIT_MAX
]) < 2)
2689 nla_for_each_nested(mode
, tb_limit
[NL80211_IFACE_LIMIT_TYPES
], mode_rem
) {
2690 if (nla_type(mode
) == NL80211_IFTYPE_AP
)
2699 static int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
2701 struct nl80211_msg_conveyor
*req
;
2703 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
2707 nl80211_send(req
, nl80211_get_ifcomb_cb
, buf
);
2712 static int nl80211_get_hardware_id(const char *ifname
, char *buf
)
2717 /* Got a radioX pseudo interface, find some interface on it or create one */
2718 if (!strncmp(ifname
, "radio", 5))
2720 /* Reuse existing interface */
2721 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
2723 rv
= wext_ops
.hardware_id(res
, buf
);
2726 /* Need to spawn a temporary iface for finding IDs */
2727 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
2729 rv
= wext_ops
.hardware_id(res
, buf
);
2735 rv
= wext_ops
.hardware_id(ifname
, buf
);
2738 /* Failed to obtain hardware IDs, search board config */
2741 rv
= iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id
*)buf
);
2747 static const struct iwinfo_hardware_entry
*
2748 nl80211_get_hardware_entry(const char *ifname
)
2750 struct iwinfo_hardware_id id
;
2752 if (nl80211_get_hardware_id(ifname
, (char *)&id
))
2755 return iwinfo_hardware(&id
);
2758 static int nl80211_get_hardware_name(const char *ifname
, char *buf
)
2760 const struct iwinfo_hardware_entry
*hw
;
2762 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
2763 sprintf(buf
, "Generic MAC80211");
2765 sprintf(buf
, "%s %s", hw
->vendor_name
, hw
->device_name
);
2770 static int nl80211_get_txpower_offset(const char *ifname
, int *buf
)
2772 const struct iwinfo_hardware_entry
*hw
;
2774 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
2777 *buf
= hw
->txpower_offset
;
2781 static int nl80211_get_frequency_offset(const char *ifname
, int *buf
)
2783 const struct iwinfo_hardware_entry
*hw
;
2785 if (!(hw
= nl80211_get_hardware_entry(ifname
)))
2788 *buf
= hw
->frequency_offset
;
2792 static int nl80211_lookup_phyname(const char *section
, char *buf
)
2796 if ((idx
= nl80211_phy_idx_from_uci(section
)) < 0)
2799 sprintf(buf
, "phy%d", idx
);
2803 const struct iwinfo_ops nl80211_ops
= {
2805 .probe
= nl80211_probe
,
2806 .channel
= nl80211_get_channel
,
2807 .frequency
= nl80211_get_frequency
,
2808 .frequency_offset
= nl80211_get_frequency_offset
,
2809 .txpower
= nl80211_get_txpower
,
2810 .txpower_offset
= nl80211_get_txpower_offset
,
2811 .bitrate
= nl80211_get_bitrate
,
2812 .signal
= nl80211_get_signal
,
2813 .noise
= nl80211_get_noise
,
2814 .quality
= nl80211_get_quality
,
2815 .quality_max
= nl80211_get_quality_max
,
2816 .mbssid_support
= nl80211_get_mbssid_support
,
2817 .hwmodelist
= nl80211_get_hwmodelist
,
2818 .htmodelist
= nl80211_get_htmodelist
,
2819 .mode
= nl80211_get_mode
,
2820 .ssid
= nl80211_get_ssid
,
2821 .bssid
= nl80211_get_bssid
,
2822 .country
= nl80211_get_country
,
2823 .hardware_id
= nl80211_get_hardware_id
,
2824 .hardware_name
= nl80211_get_hardware_name
,
2825 .encryption
= nl80211_get_encryption
,
2826 .phyname
= nl80211_get_phyname
,
2827 .assoclist
= nl80211_get_assoclist
,
2828 .txpwrlist
= nl80211_get_txpwrlist
,
2829 .scanlist
= nl80211_get_scanlist
,
2830 .freqlist
= nl80211_get_freqlist
,
2831 .countrylist
= nl80211_get_countrylist
,
2832 .lookup_phy
= nl80211_lookup_phyname
,
2833 .close
= nl80211_close