2 * iwinfo - Wireless Information Library - NL80211 Backend
4 * Copyright (C) 2010 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.
25 #include "iwinfo_nl80211.h"
26 #include "iwinfo_wext.h"
28 #define min(x, y) ((x) < (y)) ? (x) : (y)
30 extern struct iwinfo_iso3166_label ISO3166_Names
[];
31 static struct nl80211_state
*nls
= NULL
;
33 static int nl80211_init(void)
39 nls
= malloc(sizeof(struct nl80211_state
));
45 nls
->nl_sock
= nl_socket_alloc();
51 if( genl_connect(nls
->nl_sock
)) {
56 fd
= nl_socket_get_fd(nls
->nl_sock
);
57 if (fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
) < 0)
63 if( genl_ctrl_alloc_cache(nls
->nl_sock
, &nls
->nl_cache
)) {
68 nls
->nl80211
= genl_ctrl_search_by_name(nls
->nl_cache
, "nl80211");
84 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
85 struct nlmsgerr
*err
, void *arg
)
92 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
99 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
106 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
111 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
126 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
, int cmd
, int flags
)
128 static struct nl80211_msg_conveyor cv
;
130 int ifidx
= -1, phyidx
= -1;
131 struct nl_msg
*req
= NULL
;
132 struct nl_cb
*cb
= NULL
;
134 if (nl80211_init() < 0)
137 if (!strncmp(ifname
, "phy", 3))
138 phyidx
= atoi(&ifname
[3]);
139 else if (!strncmp(ifname
, "radio", 5))
140 phyidx
= atoi(&ifname
[5]);
141 else if (!strncmp(ifname
, "mon.", 4))
142 ifidx
= if_nametoindex(&ifname
[4]);
144 ifidx
= if_nametoindex(ifname
);
146 if ((ifidx
< 0) && (phyidx
< 0))
153 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
157 genlmsg_put(req
, 0, 0, genl_family_get_id(nls
->nl80211
), 0,
161 NLA_PUT_U32(req
, NL80211_ATTR_IFINDEX
, ifidx
);
164 NLA_PUT_U32(req
, NL80211_ATTR_WIPHY
, phyidx
);
182 static struct nl80211_msg_conveyor
* nl80211_send(
183 struct nl80211_msg_conveyor
*cv
,
184 int (*cb_func
)(struct nl_msg
*, void *), void *cb_arg
186 static struct nl80211_msg_conveyor rcv
;
190 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_func
, cb_arg
);
192 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
194 if (nl_send_auto_complete(nls
->nl_sock
, cv
->msg
) < 0)
197 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
198 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
199 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
202 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
213 static struct nlattr
** nl80211_parse(struct nl_msg
*msg
)
215 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
216 static struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
218 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
219 genlmsg_attrlen(gnlh
, 0), NULL
);
224 static int nl80211_freq2channel(int freq
)
230 return (freq
- 2407) / 5;
232 return (freq
/ 5) - 1000;
235 static char * nl80211_getval(const char *ifname
, const char *buf
, const char *key
)
238 char lkey
[64] = { 0 };
239 const char *ln
= buf
;
240 static char lval
[256] = { 0 };
242 int matched_if
= ifname
? 0 : 1;
245 for( i
= 0, len
= strlen(buf
); i
< len
; i
++ )
247 if (!lkey
[0] && (buf
[i
] == ' ' || buf
[i
] == '\t'))
251 else if (!lkey
[0] && (buf
[i
] == '='))
253 if ((&buf
[i
] - ln
) > 0)
254 memcpy(lkey
, ln
, min(sizeof(lkey
) - 1, &buf
[i
] - ln
));
256 else if (buf
[i
] == '\n')
260 memcpy(lval
, ln
+ strlen(lkey
) + 1,
261 min(sizeof(lval
) - 1, &buf
[i
] - ln
- strlen(lkey
) - 1));
263 if ((ifname
!= NULL
) &&
264 (!strcmp(lkey
, "interface") || !strcmp(lkey
, "bss")) )
266 matched_if
= !strcmp(lval
, ifname
);
268 else if (matched_if
&& !strcmp(lkey
, key
))
275 memset(lkey
, 0, sizeof(lkey
));
276 memset(lval
, 0, sizeof(lval
));
283 static int nl80211_ifname2phy_cb(struct nl_msg
*msg
, void *arg
)
286 struct nlattr
**attr
= nl80211_parse(msg
);
288 if (attr
[NL80211_ATTR_WIPHY_NAME
])
289 sprintf(buf
, "%s", nla_data(attr
[NL80211_ATTR_WIPHY_NAME
]));
296 static char * nl80211_ifname2phy(const char *ifname
)
298 static char phy
[32] = { 0 };
299 struct nl80211_msg_conveyor
*req
;
301 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
304 nl80211_send(req
, nl80211_ifname2phy_cb
, phy
);
308 return phy
[0] ? phy
: NULL
;
311 static char * nl80211_hostapd_info(const char *ifname
)
314 char path
[32] = { 0 };
315 static char buf
[4096] = { 0 };
318 if ((phy
= nl80211_ifname2phy(ifname
)) != NULL
)
320 snprintf(path
, sizeof(path
), "/var/run/hostapd-%s.conf", phy
);
322 if ((conf
= fopen(path
, "r")) != NULL
)
324 fread(buf
, sizeof(buf
) - 1, 1, conf
);
334 static inline int nl80211_wpactl_recv(int sock
, char *buf
, int blen
)
337 struct timeval tv
= { 2, 0 };
342 memset(buf
, 0, blen
);
345 if (select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0)
348 if (!FD_ISSET(sock
, &rfds
))
351 return recv(sock
, buf
, blen
, 0);
354 static char * nl80211_wpactl_info(const char *ifname
, const char *cmd
,
360 size_t remote_length
, local_length
;
361 static char buffer
[10240] = { 0 };
363 struct sockaddr_un local
= { 0 };
364 struct sockaddr_un remote
= { 0 };
367 sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
371 remote
.sun_family
= AF_UNIX
;
372 remote_length
= sizeof(remote
.sun_family
) + sprintf(remote
.sun_path
,
373 "/var/run/wpa_supplicant-%s/%s", ifname
, ifname
);
375 if (fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0)
378 if (connect(sock
, (struct sockaddr
*) &remote
, remote_length
))
381 local
.sun_family
= AF_UNIX
;
382 local_length
= sizeof(local
.sun_family
) + sprintf(local
.sun_path
,
383 "/var/run/iwinfo-%s-%d", ifname
, getpid());
385 if (bind(sock
, (struct sockaddr
*) &local
, local_length
))
389 send(sock
, "ATTACH", 6, 0);
391 if (nl80211_wpactl_recv(sock
, buffer
, sizeof(buffer
)) <= 0)
395 send(sock
, cmd
, strlen(cmd
), 0);
397 while( numtry
++ < 5 )
399 if (nl80211_wpactl_recv(sock
, buffer
, sizeof(buffer
)) <= 0)
407 if ((!event
&& buffer
[0] != '<') || strstr(buffer
, event
))
416 if (local
.sun_family
)
417 unlink(local
.sun_path
);
422 static inline int nl80211_readint(const char *path
)
428 if ((fd
= open(path
, O_RDONLY
)) > -1)
430 if (read(fd
, buffer
, sizeof(buffer
)) > 0)
439 static char * nl80211_phy2ifname(const char *ifname
)
441 int fd
, ifidx
= -1, cifidx
= -1, phyidx
= -1;
443 static char nif
[IFNAMSIZ
] = { 0 };
450 else if (!strncmp(ifname
, "phy", 3))
451 phyidx
= atoi(&ifname
[3]);
452 else if (!strncmp(ifname
, "radio", 5))
453 phyidx
= atoi(&ifname
[5]);
457 if ((d
= opendir("/sys/class/net")) != NULL
)
459 while( (e
= readdir(d
)) != NULL
)
461 snprintf(buffer
, sizeof(buffer
),
462 "/sys/class/net/%s/phy80211/index", e
->d_name
);
464 if (nl80211_readint(buffer
) == phyidx
)
466 snprintf(buffer
, sizeof(buffer
),
467 "/sys/class/net/%s/ifindex", e
->d_name
);
469 if( (cifidx
= nl80211_readint(buffer
)) >= 0 &&
470 ((ifidx
< 0) || (cifidx
< ifidx
)) )
473 strncpy(nif
, e
->d_name
, sizeof(nif
));
482 return nif
[0] ? nif
: NULL
;
485 static char * nl80211_ifadd(const char *ifname
)
489 static char nif
[IFNAMSIZ
] = { 0 };
490 struct nl80211_msg_conveyor
*req
, *res
;
492 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
495 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
497 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
498 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
500 nl80211_send(req
, NULL
, NULL
);
511 static void nl80211_ifdel(const char *ifname
)
513 struct nl80211_msg_conveyor
*req
;
515 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
518 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
520 nl80211_send(req
, NULL
, NULL
);
527 static void nl80211_hostapd_hup(const char *ifname
)
531 char *phy
= nl80211_ifname2phy(ifname
);
535 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
536 if ((fd
= open(buf
, O_RDONLY
)) > 0)
538 if (read(fd
, buf
, sizeof(buf
)) > 0)
550 int nl80211_probe(const char *ifname
)
552 return !!nl80211_ifname2phy(ifname
);
555 void nl80211_close(void)
560 nl_socket_free(nls
->nl_sock
);
563 nl_cache_free(nls
->nl_cache
);
570 int nl80211_get_mode(const char *ifname
, char *buf
)
572 return wext_get_mode(ifname
, buf
);
575 int nl80211_get_ssid(const char *ifname
, char *buf
)
579 if (!wext_get_ssid(ifname
, buf
))
583 else if( (ssid
= nl80211_hostapd_info(ifname
)) &&
584 (ssid
= nl80211_getval(ifname
, ssid
, "ssid")) )
586 memcpy(buf
, ssid
, strlen(ssid
));
593 int nl80211_get_bssid(const char *ifname
, char *buf
)
596 unsigned char mac
[6];
598 if (!wext_get_bssid(ifname
, buf
))
602 else if((bssid
= nl80211_hostapd_info(ifname
)) &&
603 (bssid
= nl80211_getval(ifname
, bssid
, "bssid")))
605 mac
[0] = strtol(&bssid
[0], NULL
, 16);
606 mac
[1] = strtol(&bssid
[3], NULL
, 16);
607 mac
[2] = strtol(&bssid
[6], NULL
, 16);
608 mac
[3] = strtol(&bssid
[9], NULL
, 16);
609 mac
[4] = strtol(&bssid
[12], NULL
, 16);
610 mac
[5] = strtol(&bssid
[15], NULL
, 16);
612 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
613 mac
[0], mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
621 int nl80211_get_channel(const char *ifname
, int *buf
)
625 if (!wext_get_channel(ifname
, buf
))
628 else if ((first
= nl80211_phy2ifname(nl80211_ifname2phy(ifname
))) != NULL
)
629 return wext_get_channel(first
, buf
);
634 int nl80211_get_frequency(const char *ifname
, int *buf
)
638 if (!wext_get_frequency(ifname
, buf
))
641 else if ((first
= nl80211_phy2ifname(nl80211_ifname2phy(ifname
))) != NULL
)
642 return wext_get_frequency(first
, buf
);
647 int nl80211_get_txpower(const char *ifname
, int *buf
)
649 return wext_get_txpower(ifname
, buf
);
653 static int nl80211_fill_signal_cb(struct nl_msg
*msg
, void *arg
)
657 struct nl80211_rssi_rate
*rr
= arg
;
658 struct nlattr
**attr
= nl80211_parse(msg
);
659 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
660 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
662 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
663 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
664 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
665 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
666 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
667 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
668 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
669 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
670 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
671 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
672 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
675 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
676 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
677 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
678 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
679 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
682 if (attr
[NL80211_ATTR_STA_INFO
])
684 if( !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
685 attr
[NL80211_ATTR_STA_INFO
], stats_policy
) )
687 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
689 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
690 rr
->rssi
= rr
->rssi
? (int8_t)((rr
->rssi
+ dbm
) / 2) : dbm
;
693 if (sinfo
[NL80211_STA_INFO_TX_BITRATE
])
695 if( !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
696 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
) )
698 if (rinfo
[NL80211_RATE_INFO_BITRATE
])
700 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
702 ? (int16_t)((rr
->rate
+ mbit
) / 2) : mbit
;
712 static void nl80211_fill_signal(const char *ifname
, struct nl80211_rssi_rate
*r
)
716 struct nl80211_msg_conveyor
*req
;
721 if ((d
= opendir("/sys/class/net")) != NULL
)
723 while ((de
= readdir(d
)) != NULL
)
725 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
726 (!de
->d_name
[strlen(ifname
)] ||
727 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
729 req
= nl80211_msg(de
->d_name
, NL80211_CMD_GET_STATION
,
734 nl80211_send(req
, nl80211_fill_signal_cb
, r
);
744 int nl80211_get_bitrate(const char *ifname
, int *buf
)
746 struct nl80211_rssi_rate rr
;
748 if (!wext_get_bitrate(ifname
, buf
))
751 nl80211_fill_signal(ifname
, &rr
);
755 *buf
= (rr
.rate
* 100);
762 int nl80211_get_signal(const char *ifname
, int *buf
)
764 struct nl80211_rssi_rate rr
;
766 if (!wext_get_signal(ifname
, buf
))
769 nl80211_fill_signal(ifname
, &rr
);
780 static int nl80211_get_noise_cb(struct nl_msg
*msg
, void *arg
)
783 struct nlattr
**tb
= nl80211_parse(msg
);
784 struct nlattr
*si
[NL80211_SURVEY_INFO_MAX
+ 1];
786 static struct nla_policy sp
[NL80211_SURVEY_INFO_MAX
+ 1] = {
787 [NL80211_SURVEY_INFO_FREQUENCY
] = { .type
= NLA_U32
},
788 [NL80211_SURVEY_INFO_NOISE
] = { .type
= NLA_U8
},
791 if (!tb
[NL80211_ATTR_SURVEY_INFO
])
794 if (nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
795 tb
[NL80211_ATTR_SURVEY_INFO
], sp
))
798 if (!si
[NL80211_SURVEY_INFO_NOISE
])
801 if (!*noise
|| si
[NL80211_SURVEY_INFO_IN_USE
])
802 *noise
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
808 int nl80211_get_noise(const char *ifname
, int *buf
)
811 struct nl80211_msg_conveyor
*req
;
813 req
= nl80211_msg(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
);
818 nl80211_send(req
, nl80211_get_noise_cb
, &noise
);
831 int nl80211_get_quality(const char *ifname
, int *buf
)
835 if (wext_get_quality(ifname
, buf
))
839 if (!nl80211_get_signal(ifname
, &signal
))
841 /* A positive signal level is usually just a quality
842 * value, pass through as-is */
848 /* The cfg80211 wext compat layer assumes a signal range
849 * of -110 dBm to -40 dBm, the quality value is derived
850 * by adding 110 to the signal level */
855 else if (signal
> -40)
858 *buf
= (signal
+ 110);
866 int nl80211_get_quality_max(const char *ifname
, int *buf
)
868 if (wext_get_quality_max(ifname
, buf
))
869 /* The cfg80211 wext compat layer assumes a maximum
876 int nl80211_get_encryption(const char *ifname
, char *buf
)
881 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
884 if( (res
= nl80211_wpactl_info(ifname
, "STATUS", NULL
)) &&
885 (val
= nl80211_getval(NULL
, res
, "pairwise_cipher")) )
888 if (strstr(val
, "WEP"))
890 if (strstr(val
, "WEP-40"))
891 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
893 else if (strstr(val
, "WEP-104"))
894 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
897 c
->group_ciphers
= c
->pair_ciphers
;
899 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
900 c
->auth_algs
|= IWINFO_AUTH_OPEN
; /* XXX: assumption */
906 if (strstr(val
, "TKIP"))
907 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
909 else if (strstr(val
, "CCMP"))
910 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
912 else if (strstr(val
, "NONE"))
913 c
->pair_ciphers
|= IWINFO_CIPHER_NONE
;
915 else if (strstr(val
, "WEP-40"))
916 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
918 else if (strstr(val
, "WEP-104"))
919 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
922 if ((val
= nl80211_getval(NULL
, res
, "group_cipher")))
924 if (strstr(val
, "TKIP"))
925 c
->group_ciphers
|= IWINFO_CIPHER_TKIP
;
927 else if (strstr(val
, "CCMP"))
928 c
->group_ciphers
|= IWINFO_CIPHER_CCMP
;
930 else if (strstr(val
, "NONE"))
931 c
->group_ciphers
|= IWINFO_CIPHER_NONE
;
933 else if (strstr(val
, "WEP-40"))
934 c
->group_ciphers
|= IWINFO_CIPHER_WEP40
;
936 else if (strstr(val
, "WEP-104"))
937 c
->group_ciphers
|= IWINFO_CIPHER_WEP104
;
941 if ((val
= nl80211_getval(NULL
, res
, "key_mgmt")))
943 if (strstr(val
, "WPA2"))
946 else if (strstr(val
, "WPA"))
950 if (strstr(val
, "PSK"))
951 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
953 else if (strstr(val
, "EAP") || strstr(val
, "802.1X"))
954 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
956 else if (strstr(val
, "NONE"))
957 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
960 c
->enabled
= (c
->wpa_version
&& c
->auth_suites
) ? 1 : 0;
967 else if ((res
= nl80211_hostapd_info(ifname
)))
969 if ((val
= nl80211_getval(ifname
, res
, "wpa")) != NULL
)
970 c
->wpa_version
= atoi(val
);
972 val
= nl80211_getval(ifname
, res
, "wpa_key_mgmt");
974 if (!val
|| strstr(val
, "PSK"))
975 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
977 if (val
&& strstr(val
, "EAP"))
978 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
980 if (val
&& strstr(val
, "NONE"))
981 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
983 if ((val
= nl80211_getval(ifname
, res
, "wpa_pairwise")) != NULL
)
985 if (strstr(val
, "TKIP"))
986 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
988 if (strstr(val
, "CCMP"))
989 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
991 if (strstr(val
, "NONE"))
992 c
->pair_ciphers
|= IWINFO_CIPHER_NONE
;
995 if ((val
= nl80211_getval(ifname
, res
, "auth_algs")) != NULL
)
999 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1003 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1007 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
1008 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
1015 for( i
= 0; i
< 4; i
++ )
1017 snprintf(k
, sizeof(k
), "wep_key%d", i
);
1019 if ((val
= nl80211_getval(ifname
, res
, k
)))
1021 if ((strlen(val
) == 5) || (strlen(val
) == 10))
1022 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
1024 else if ((strlen(val
) == 13) || (strlen(val
) == 26))
1025 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
1030 c
->group_ciphers
= c
->pair_ciphers
;
1031 c
->enabled
= (c
->auth_algs
|| c
->auth_suites
) ? 1 : 0;
1040 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
1042 struct nl80211_array_buf
*arr
= arg
;
1043 struct iwinfo_assoclist_entry
*e
= arr
->buf
;
1044 struct nlattr
**attr
= nl80211_parse(msg
);
1045 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1047 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1048 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1049 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1050 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1051 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1052 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1053 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1054 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1055 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1056 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1057 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1060 /* advance to end of array */
1063 if (attr
[NL80211_ATTR_MAC
])
1064 memcpy(e
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
1066 if (attr
[NL80211_ATTR_STA_INFO
])
1068 if (!nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1069 attr
[NL80211_ATTR_STA_INFO
], stats_policy
))
1071 if (sinfo
[NL80211_STA_INFO_SIGNAL
])
1072 e
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1076 e
->noise
= 0; /* filled in by caller */
1082 int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
1087 struct nl80211_msg_conveyor
*req
;
1088 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
1089 struct iwinfo_assoclist_entry
*e
;
1091 if ((d
= opendir("/sys/class/net")) != NULL
)
1093 while ((de
= readdir(d
)) != NULL
)
1095 if (!strncmp(de
->d_name
, ifname
, strlen(ifname
)) &&
1096 (!de
->d_name
[strlen(ifname
)] ||
1097 !strncmp(&de
->d_name
[strlen(ifname
)], ".sta", 4)))
1099 req
= nl80211_msg(de
->d_name
, NL80211_CMD_GET_STATION
,
1104 nl80211_send(req
, nl80211_get_assoclist_cb
, &arr
);
1114 if (!nl80211_get_noise(ifname
, &noise
))
1115 for (i
= 0, e
= arr
.buf
; i
< arr
.count
; i
++, e
++)
1118 *len
= (arr
.count
* sizeof(struct iwinfo_assoclist_entry
));
1125 static int nl80211_get_txpwrlist_cb(struct nl_msg
*msg
, void *arg
)
1128 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
1130 struct nlattr
**attr
= nl80211_parse(msg
);
1131 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
1132 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1133 struct nlattr
*band
, *freq
;
1135 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
1136 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
1137 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
1138 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
1139 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
1140 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
1141 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
1144 ch_cur
= *dbm_max
; /* value int* is initialized with channel by caller */
1147 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
1149 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
1150 nla_len(band
), NULL
);
1152 nla_for_each_nested(freq
,
1153 bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
1155 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
1156 nla_data(freq
), nla_len(freq
), freq_policy
);
1158 ch_cmp
= nl80211_freq2channel(
1159 nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
1161 if( (!ch_cur
|| (ch_cmp
== ch_cur
)) &&
1162 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] )
1164 *dbm_max
= (int)(0.01 * nla_get_u32(
1165 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
1175 int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
1178 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
1179 struct nl80211_msg_conveyor
*req
;
1180 struct iwinfo_txpwrlist_entry entry
;
1182 if (nl80211_get_channel(ifname
, &ch_cur
))
1185 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
1188 /* initialize the value pointer with channel for callback */
1191 nl80211_send(req
, nl80211_get_txpwrlist_cb
, &dbm_max
);
1197 for (dbm_cur
= 0, dbm_cnt
= 0;
1199 dbm_cur
++, dbm_cnt
++)
1201 entry
.dbm
= dbm_cur
;
1202 entry
.mw
= iwinfo_dbm2mw(dbm_cur
);
1204 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
1207 entry
.dbm
= dbm_max
;
1208 entry
.mw
= iwinfo_dbm2mw(dbm_max
);
1210 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
1213 *len
= dbm_cnt
* sizeof(entry
);
1220 static void nl80211_get_scancrypto(const char *spec
,
1221 struct iwinfo_crypto_entry
*c
)
1223 if (strstr(spec
, "WPA") || strstr(spec
, "WEP"))
1227 if (strstr(spec
, "WPA2-") && strstr(spec
, "WPA-"))
1230 else if (strstr(spec
, "WPA2"))
1233 else if (strstr(spec
, "WPA"))
1236 else if (strstr(spec
, "WEP"))
1237 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1240 if (strstr(spec
, "PSK"))
1241 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1243 if (strstr(spec
, "802.1X") || strstr(spec
, "EAP"))
1244 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
1246 if (strstr(spec
, "WPA-NONE"))
1247 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1250 if (strstr(spec
, "TKIP"))
1251 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
1253 if (strstr(spec
, "CCMP"))
1254 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
1256 if (strstr(spec
, "WEP-40"))
1257 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
1259 if (strstr(spec
, "WEP-104"))
1260 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
1262 c
->group_ciphers
= c
->pair_ciphers
;
1270 int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
1272 int freq
, rssi
, qmax
, count
;
1274 char ssid
[128] = { 0 };
1275 char bssid
[18] = { 0 };
1276 char cipher
[256] = { 0 };
1278 /* Got a radioX pseudo interface, find some interface on it or create one */
1279 if (!strncmp(ifname
, "radio", 5))
1281 /* Reuse existing interface */
1282 if ((res
= nl80211_phy2ifname(ifname
)) != NULL
)
1284 return nl80211_get_scanlist(res
, buf
, len
);
1287 /* Need to spawn a temporary iface for scanning */
1288 else if ((res
= nl80211_ifadd(ifname
)) != NULL
)
1290 count
= nl80211_get_scanlist(res
, buf
, len
);
1296 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
1298 /* WPA supplicant */
1299 if ((res
= nl80211_wpactl_info(ifname
, "SCAN", "CTRL-EVENT-SCAN-RESULTS")))
1301 if ((res
= nl80211_wpactl_info(ifname
, "SCAN_RESULTS", NULL
)))
1303 nl80211_get_quality_max(ifname
, &qmax
);
1305 /* skip header line */
1306 while( *res
++ != '\n' );
1310 while( sscanf(res
, "%17s %d %d %255s%*[ \t]%127[^\n]\n",
1311 bssid
, &freq
, &rssi
, cipher
, ssid
) > 0 )
1314 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
1315 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
1316 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
1317 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
1318 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
1319 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
1322 memcpy(e
->ssid
, ssid
,
1323 min(strlen(ssid
), sizeof(e
->ssid
) - 1));
1325 /* Mode (assume master) */
1326 sprintf((char *)e
->mode
, "Master");
1329 e
->channel
= nl80211_freq2channel(freq
);
1337 /* The cfg80211 wext compat layer assumes a signal range
1338 * of -110 dBm to -40 dBm, the quality value is derived
1339 * by adding 110 to the signal level */
1342 else if (rssi
> -40)
1345 e
->quality
= (rssi
+ 110);
1353 e
->quality_max
= qmax
;
1356 nl80211_get_scancrypto(cipher
, &e
->crypto
);
1358 /* advance to next line */
1359 while( *res
&& *res
++ != '\n' );
1364 memset(ssid
, 0, sizeof(ssid
));
1365 memset(bssid
, 0, sizeof(bssid
));
1366 memset(cipher
, 0, sizeof(cipher
));
1369 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
1377 /* Got a temp interface, don't create yet another one */
1378 if (!strncmp(ifname
, "tmp.", 4))
1380 if (!iwinfo_ifup(ifname
))
1383 wext_get_scanlist(ifname
, buf
, len
);
1384 iwinfo_ifdown(ifname
);
1388 /* Spawn a new scan interface */
1391 if (!(res
= nl80211_ifadd(ifname
)))
1394 if (!iwinfo_ifmac(res
))
1397 /* if we can take the new interface up, the driver supports an
1398 * additional interface and there's no need to tear down the ap */
1399 if (iwinfo_ifup(res
))
1401 wext_get_scanlist(res
, buf
, len
);
1405 /* driver cannot create secondary interface, take down ap
1407 else if (iwinfo_ifdown(ifname
) && iwinfo_ifup(res
))
1409 wext_get_scanlist(res
, buf
, len
);
1411 iwinfo_ifup(ifname
);
1412 nl80211_hostapd_hup(ifname
);
1424 static int nl80211_get_freqlist_cb(struct nl_msg
*msg
, void *arg
)
1426 int bands_remain
, freqs_remain
;
1428 struct nl80211_array_buf
*arr
= arg
;
1429 struct iwinfo_freqlist_entry
*e
= arr
->buf
;
1431 struct nlattr
**attr
= nl80211_parse(msg
);
1432 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
1433 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1434 struct nlattr
*band
, *freq
;
1436 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
1437 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
1438 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
1439 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
1440 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
1441 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
1442 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
1445 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
1447 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
1448 nla_len(band
), NULL
);
1450 nla_for_each_nested(freq
,
1451 bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
1453 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
1454 nla_data(freq
), nla_len(freq
), NULL
);
1456 if( !freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
1457 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
] )
1460 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
1461 e
->channel
= nl80211_freq2channel(e
->mhz
);
1464 freqs
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] ||
1465 freqs
[NL80211_FREQUENCY_ATTR_NO_IBSS
] ||
1466 freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
1477 int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
1479 struct nl80211_msg_conveyor
*req
;
1480 struct nl80211_array_buf arr
= { .buf
= buf
, .count
= 0 };
1482 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
1485 nl80211_send(req
, nl80211_get_freqlist_cb
, &arr
);
1491 *len
= arr
.count
* sizeof(struct iwinfo_freqlist_entry
);
1498 static int nl80211_get_country_cb(struct nl_msg
*msg
, void *arg
)
1501 struct nlattr
**attr
= nl80211_parse(msg
);
1503 if (attr
[NL80211_ATTR_REG_ALPHA2
])
1504 memcpy(buf
, nla_data(attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
1511 int nl80211_get_country(const char *ifname
, char *buf
)
1514 struct nl80211_msg_conveyor
*req
;
1516 req
= nl80211_msg(ifname
, NL80211_CMD_GET_REG
, 0);
1519 nl80211_send(req
, nl80211_get_country_cb
, buf
);
1529 int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
1532 struct iwinfo_iso3166_label
*l
;
1533 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
1535 for( l
= ISO3166_Names
, count
= 0; l
->iso3166
; l
++, e
++, count
++ )
1537 e
->iso3166
= l
->iso3166
;
1538 e
->ccode
[0] = (l
->iso3166
/ 256);
1539 e
->ccode
[1] = (l
->iso3166
% 256);
1542 *len
= (count
* sizeof(struct iwinfo_country_entry
));
1546 static int nl80211_get_hwmodelist_cb(struct nl_msg
*msg
, void *arg
)
1549 int bands_remain
, freqs_remain
;
1551 struct nlattr
**attr
= nl80211_parse(msg
);
1552 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
1553 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1554 struct nlattr
*band
, *freq
;
1558 if (attr
[NL80211_ATTR_WIPHY_BANDS
])
1560 nla_for_each_nested(band
, attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
1562 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
1563 nla_len(band
), NULL
);
1565 if (bands
[NL80211_BAND_ATTR_HT_CAPA
])
1566 caps
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
1568 /* Treat any nonzero capability as 11n */
1570 *modes
|= IWINFO_80211_N
;
1572 nla_for_each_nested(freq
,
1573 bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
1575 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
1576 nla_data(freq
), nla_len(freq
), NULL
);
1578 if (!freqs
[NL80211_FREQUENCY_ATTR_FREQ
])
1581 if (nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]) < 2485)
1583 *modes
|= IWINFO_80211_B
;
1584 *modes
|= IWINFO_80211_G
;
1588 *modes
|= IWINFO_80211_A
;
1597 int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
1599 struct nl80211_msg_conveyor
*req
;
1601 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
1604 nl80211_send(req
, nl80211_get_hwmodelist_cb
, buf
);
1608 return *buf
? 0 : -1;
1611 int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
1613 /* Test whether we can create another interface */
1614 char *nif
= nl80211_ifadd(ifname
);
1618 *buf
= (iwinfo_ifmac(nif
) && iwinfo_ifup(nif
));