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
;
32 static int nl80211_ioctlsock
= -1;
34 static int nl80211_init(void)
40 nl80211_ioctlsock
= socket(AF_INET
, SOCK_DGRAM
, 0);
41 if( nl80211_ioctlsock
< 0 )
46 else if( fcntl(nl80211_ioctlsock
, F_SETFD
,
47 fcntl(nl80211_ioctlsock
, F_GETFD
) | FD_CLOEXEC
) < 0 )
53 nls
= malloc(sizeof(struct nl80211_state
));
59 nls
->nl_sock
= nl_socket_alloc();
65 if( genl_connect(nls
->nl_sock
)) {
70 fd
= nl_socket_get_fd(nls
->nl_sock
);
71 if( fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
) < 0 )
77 if( genl_ctrl_alloc_cache(nls
->nl_sock
, &nls
->nl_cache
)) {
82 nls
->nl80211
= genl_ctrl_search_by_name(nls
->nl_cache
, "nl80211");
98 static int nl80211_msg_error(struct sockaddr_nl
*nla
,
99 struct nlmsgerr
*err
, void *arg
)
106 static int nl80211_msg_finish(struct nl_msg
*msg
, void *arg
)
113 static int nl80211_msg_ack(struct nl_msg
*msg
, void *arg
)
120 static int nl80211_msg_response(struct nl_msg
*msg
, void *arg
)
122 struct nl80211_msg_conveyor
*cv
= arg
;
127 cv
->hdr
= nlmsg_data(nlmsg_hdr(cv
->msg
));
129 nla_parse(cv
->attr
, NL80211_ATTR_MAX
,
130 genlmsg_attrdata(cv
->hdr
, 0),
131 genlmsg_attrlen(cv
->hdr
, 0), NULL
);
136 static void nl80211_free(struct nl80211_msg_conveyor
*cv
)
151 static struct nl80211_msg_conveyor
* nl80211_msg(const char *ifname
, int cmd
, int flags
)
153 static struct nl80211_msg_conveyor cv
;
155 int ifidx
= -1, phyidx
= -1;
156 struct nl_msg
*req
= NULL
;
157 struct nl_cb
*cb
= NULL
;
159 if( nl80211_init() < 0 )
162 if( !strncmp(ifname
, "radio", 5) )
163 phyidx
= atoi(&ifname
[5]);
164 else if( !strncmp(ifname
, "mon.", 4) )
165 ifidx
= if_nametoindex(&ifname
[4]);
167 ifidx
= if_nametoindex(ifname
);
169 if( (ifidx
< 0) && (phyidx
< 0) )
176 cb
= nl_cb_alloc(NL_CB_DEFAULT
);
180 genlmsg_put(req
, 0, 0, genl_family_get_id(nls
->nl80211
), 0,
184 NLA_PUT_U32(req
, NL80211_ATTR_IFINDEX
, ifidx
);
187 NLA_PUT_U32(req
, NL80211_ATTR_WIPHY
, phyidx
);
208 static void nl80211_cb(struct nl80211_msg_conveyor
*cv
,
209 int (*cb
)(struct nl_msg
*, void *), void *arg
)
212 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb
, arg
);
215 static struct nl80211_msg_conveyor
* nl80211_send(struct nl80211_msg_conveyor
*cv
)
217 static struct nl80211_msg_conveyor rcv
;
221 nl_cb_set(cv
->cb
, NL_CB_VALID
, NL_CB_CUSTOM
, nl80211_msg_response
, &rcv
);
223 if( nl_send_auto_complete(nls
->nl_sock
, cv
->msg
) < 0 )
226 nl_cb_err(cv
->cb
, NL_CB_CUSTOM
, nl80211_msg_error
, &err
);
227 nl_cb_set(cv
->cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, nl80211_msg_finish
, &err
);
228 nl_cb_set(cv
->cb
, NL_CB_ACK
, NL_CB_CUSTOM
, nl80211_msg_ack
, &err
);
231 nl_recvmsgs(nls
->nl_sock
, cv
->cb
);
242 static int nl80211_freq2channel(int freq
)
248 return (freq
- 2407) / 5;
250 return (freq
/ 5) - 1000;
253 static char * nl80211_getval(const char *ifname
, const char *buf
, const char *key
)
256 char lkey
[64] = { 0 };
257 const char *ln
= buf
;
258 static char lval
[256] = { 0 };
260 int matched_if
= ifname
? 0 : 1;
263 for( i
= 0, len
= strlen(buf
); i
< len
; i
++ )
265 if( !lkey
[0] && (buf
[i
] == ' ' || buf
[i
] == '\t') )
269 else if( !lkey
[0] && (buf
[i
] == '=') )
271 if( (&buf
[i
] - ln
) > 0 )
272 memcpy(lkey
, ln
, min(sizeof(lkey
) - 1, &buf
[i
] - ln
));
274 else if( buf
[i
] == '\n' )
278 memcpy(lval
, ln
+ strlen(lkey
) + 1,
279 min(sizeof(lval
) - 1, &buf
[i
] - ln
- strlen(lkey
) - 1));
281 if( (ifname
!= NULL
) &&
282 (!strcmp(lkey
, "interface") || !strcmp(lkey
, "bss")) )
284 matched_if
= !strcmp(lval
, ifname
);
286 else if( matched_if
&& !strcmp(lkey
, key
) )
293 memset(lkey
, 0, sizeof(lkey
));
294 memset(lval
, 0, sizeof(lval
));
301 static char * nl80211_ifname2phy(const char *ifname
)
303 static char phy
[32] = { 0 };
304 struct nl80211_msg_conveyor
*req
, *res
;
306 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
309 res
= nl80211_send(req
);
312 if( res
->attr
[NL80211_ATTR_WIPHY_NAME
] )
314 snprintf(phy
, sizeof(phy
), "%s",
315 nla_get_string(res
->attr
[NL80211_ATTR_WIPHY_NAME
]));
322 return phy
[0] ? phy
: NULL
;
325 static char * nl80211_hostapd_info(const char *ifname
)
328 char path
[32] = { 0 };
329 static char buf
[4096] = { 0 };
332 if( (phy
= nl80211_ifname2phy(ifname
)) != NULL
)
334 snprintf(path
, sizeof(path
), "/var/run/hostapd-%s.conf", phy
);
336 if( (conf
= fopen(path
, "r")) != NULL
)
338 fread(buf
, sizeof(buf
) - 1, 1, conf
);
348 static char * nl80211_wpasupp_info(const char *ifname
, const char *cmd
)
352 size_t remote_length
, local_length
;
353 static char buffer
[1024] = { 0 };
355 struct timeval tv
= { 2, 0 };
356 struct sockaddr_un local
= { 0 };
357 struct sockaddr_un remote
= { 0 };
361 sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
365 remote
.sun_family
= AF_UNIX
;
366 remote_length
= sizeof(remote
.sun_family
) + sprintf(remote
.sun_path
,
367 "/var/run/wpa_supplicant-%s/%s", ifname
, ifname
);
369 if( fcntl(sock
, F_SETFD
, fcntl(sock
, F_GETFD
) | FD_CLOEXEC
) < 0 )
372 if( connect(sock
, (struct sockaddr
*) &remote
, remote_length
) )
375 local
.sun_family
= AF_UNIX
;
376 local_length
= sizeof(local
.sun_family
) + sprintf(local
.sun_path
,
377 "/var/run/iwinfo-%s-%d", ifname
, getpid());
379 if( bind(sock
, (struct sockaddr
*) &local
, local_length
) )
382 send(sock
, cmd
, strlen(cmd
), 0);
389 if( select(sock
+ 1, &rfds
, NULL
, NULL
, &tv
) < 0 )
392 if( !FD_ISSET(sock
, &rfds
) )
395 if( (len
= recv(sock
, buffer
, sizeof(buffer
), 0)) <= 0 )
400 if( buffer
[0] != '<' )
409 if( local
.sun_family
)
410 unlink(local
.sun_path
);
415 static char * nl80211_phy2ifname(const char *ifname
)
419 static char nif
[IFNAMSIZ
] = { 0 };
424 if( !strncmp(ifname
, "radio", 5) )
426 phyidx
= atoi(&ifname
[5]);
428 if( (d
= opendir("/sys/class/net")) != NULL
)
430 while( (e
= readdir(d
)) != NULL
)
432 snprintf(buffer
, sizeof(buffer
),
433 "/sys/class/net/%s/phy80211/index", e
->d_name
);
435 if( (fd
= open(buffer
, O_RDONLY
)) > 0 )
437 if( (read(fd
, buffer
, sizeof(buffer
)) > 0) &&
438 (atoi(buffer
) == phyidx
) )
440 strncpy(nif
, e
->d_name
, sizeof(nif
));
454 return nif
[0] ? nif
: NULL
;
457 static char * nl80211_ifadd(const char *ifname
)
461 static char nif
[IFNAMSIZ
] = { 0 };
462 struct nl80211_msg_conveyor
*req
, *res
;
464 req
= nl80211_msg(ifname
, NL80211_CMD_NEW_INTERFACE
, 0);
467 snprintf(nif
, sizeof(nif
), "tmp.%s", ifname
);
469 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, nif
);
470 NLA_PUT_U32(req
->msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_STATION
);
472 res
= nl80211_send(req
);
486 static void nl80211_ifdel(const char *ifname
)
488 struct nl80211_msg_conveyor
*req
, *res
;
490 req
= nl80211_msg(ifname
, NL80211_CMD_DEL_INTERFACE
, 0);
493 NLA_PUT_STRING(req
->msg
, NL80211_ATTR_IFNAME
, ifname
);
495 nl80211_free(nl80211_send(req
));
502 static int nl80211_ifup(const char *ifname
)
506 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
508 if( ioctl(nl80211_ioctlsock
, SIOCGIFFLAGS
, &ifr
) )
511 ifr
.ifr_flags
|= (IFF_UP
| IFF_RUNNING
);
513 return !ioctl(nl80211_ioctlsock
, SIOCSIFFLAGS
, &ifr
);
516 static int nl80211_ifdown(const char *ifname
)
520 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
522 if( ioctl(nl80211_ioctlsock
, SIOCGIFFLAGS
, &ifr
) )
525 ifr
.ifr_flags
&= ~(IFF_UP
| IFF_RUNNING
);
527 return !ioctl(nl80211_ioctlsock
, SIOCSIFFLAGS
, &ifr
);
530 static int nl80211_ifmac(const char *ifname
)
534 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
536 if( ioctl(nl80211_ioctlsock
, SIOCGIFHWADDR
, &ifr
) )
539 ifr
.ifr_hwaddr
.sa_data
[1]++;
540 ifr
.ifr_hwaddr
.sa_data
[2]++;
542 return !ioctl(nl80211_ioctlsock
, SIOCSIFHWADDR
, &ifr
);
545 static void nl80211_hostapd_hup(const char *ifname
)
549 char *phy
= strncmp(ifname
, "phy", 3) ? nl80211_ifname2phy(ifname
) : ifname
;
553 snprintf(buf
, sizeof(buf
), "/var/run/wifi-%s.pid", phy
);
554 if( (fd
= open(buf
, O_RDONLY
)) > 0 )
556 if( read(fd
, buf
, sizeof(buf
)) > 0 )
568 int nl80211_probe(const char *ifname
)
570 return !!nl80211_ifname2phy(ifname
);
573 void nl80211_close(void)
575 if( nl80211_ioctlsock
> -1 )
577 close(nl80211_ioctlsock
);
583 nl_socket_free(nls
->nl_sock
);
586 nl_cache_free(nls
->nl_cache
);
593 int nl80211_get_mode(const char *ifname
, char *buf
)
595 return wext_get_mode(ifname
, buf
);
598 int nl80211_get_ssid(const char *ifname
, char *buf
)
602 if( !wext_get_ssid(ifname
, buf
) )
606 else if( (ssid
= nl80211_hostapd_info(ifname
)) &&
607 (ssid
= nl80211_getval(ifname
, ssid
, "ssid")) )
609 memcpy(buf
, ssid
, strlen(ssid
));
616 int nl80211_get_bssid(const char *ifname
, char *buf
)
619 unsigned char mac
[6];
621 if( !wext_get_bssid(ifname
, buf
) )
625 else if( (bssid
= nl80211_hostapd_info(ifname
)) &&
626 (bssid
= nl80211_getval(ifname
, bssid
, "bssid")) )
628 mac
[0] = strtol(&bssid
[0], NULL
, 16);
629 mac
[1] = strtol(&bssid
[3], NULL
, 16);
630 mac
[2] = strtol(&bssid
[6], NULL
, 16);
631 mac
[3] = strtol(&bssid
[9], NULL
, 16);
632 mac
[4] = strtol(&bssid
[12], NULL
, 16);
633 mac
[5] = strtol(&bssid
[15], NULL
, 16);
635 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
636 mac
[0], mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
644 int nl80211_get_channel(const char *ifname
, int *buf
)
646 return wext_get_channel(ifname
, buf
);
649 int nl80211_get_frequency(const char *ifname
, int *buf
)
651 return wext_get_frequency(ifname
, buf
);
654 int nl80211_get_txpower(const char *ifname
, int *buf
)
656 return wext_get_txpower(ifname
, buf
);
660 static int nl80211_get_signal_cb(struct nl_msg
*msg
, void *arg
)
664 struct nl80211_rssi_rate
*rr
= arg
;
666 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
667 struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
668 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
669 struct nlattr
*rinfo
[NL80211_RATE_INFO_MAX
+ 1];
671 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
672 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
673 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
674 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
675 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
676 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
677 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
678 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
679 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
680 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
681 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
684 static struct nla_policy rate_policy
[NL80211_RATE_INFO_MAX
+ 1] = {
685 [NL80211_RATE_INFO_BITRATE
] = { .type
= NLA_U16
},
686 [NL80211_RATE_INFO_MCS
] = { .type
= NLA_U8
},
687 [NL80211_RATE_INFO_40_MHZ_WIDTH
] = { .type
= NLA_FLAG
},
688 [NL80211_RATE_INFO_SHORT_GI
] = { .type
= NLA_FLAG
},
691 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
692 genlmsg_attrlen(gnlh
, 0), NULL
);
694 if( attr
[NL80211_ATTR_STA_INFO
] )
696 if( !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
697 attr
[NL80211_ATTR_STA_INFO
], stats_policy
) )
699 if( sinfo
[NL80211_STA_INFO_SIGNAL
] )
701 dbm
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
702 rr
->rssi
= rr
->rssi
? (int8_t)((rr
->rssi
+ dbm
) / 2) : dbm
;
705 if( sinfo
[NL80211_STA_INFO_TX_BITRATE
] )
707 if( !nla_parse_nested(rinfo
, NL80211_RATE_INFO_MAX
,
708 sinfo
[NL80211_STA_INFO_TX_BITRATE
], rate_policy
) )
710 if( rinfo
[NL80211_RATE_INFO_BITRATE
] )
712 mbit
= nla_get_u16(rinfo
[NL80211_RATE_INFO_BITRATE
]);
714 ? (int16_t)((rr
->rate
+ mbit
) / 2) : mbit
;
724 int nl80211_get_bitrate(const char *ifname
, int *buf
)
726 struct nl80211_rssi_rate rr
;
727 struct nl80211_msg_conveyor
*req
;
729 if( !wext_get_bitrate(ifname
, buf
) )
732 req
= nl80211_msg(ifname
, NL80211_CMD_GET_STATION
, NLM_F_DUMP
);
738 nl80211_cb(req
, nl80211_get_signal_cb
, &rr
);
744 *buf
= (rr
.rate
* 100);
752 int nl80211_get_signal(const char *ifname
, int *buf
)
754 struct nl80211_rssi_rate rr
;
755 struct nl80211_msg_conveyor
*req
;
757 if( !wext_get_signal(ifname
, buf
) )
760 req
= nl80211_msg(ifname
, NL80211_CMD_GET_STATION
, NLM_F_DUMP
);
766 nl80211_cb(req
, nl80211_get_signal_cb
, &rr
);
780 int nl80211_get_noise(const char *ifname
, int *buf
)
783 struct nl80211_msg_conveyor
*req
, *res
;
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 req
= nl80211_msg(ifname
, NL80211_CMD_GET_SURVEY
, NLM_F_DUMP
);
794 res
= nl80211_send(req
);
797 if( res
->attr
[NL80211_ATTR_SURVEY_INFO
] )
799 if( !nla_parse_nested(si
, NL80211_SURVEY_INFO_MAX
,
800 res
->attr
[NL80211_ATTR_SURVEY_INFO
], sp
) &&
801 si
[NL80211_SURVEY_INFO_NOISE
] )
803 *buf
= (int8_t)nla_get_u8(si
[NL80211_SURVEY_INFO_NOISE
]);
815 int nl80211_get_quality(const char *ifname
, int *buf
)
819 if( wext_get_quality(ifname
, buf
) )
823 if( !nl80211_get_signal(ifname
, &signal
) )
825 /* A positive signal level is usually just a quality
826 * value, pass through as-is */
832 /* The cfg80211 wext compat layer assumes a signal range
833 * of -110 dBm to -40 dBm, the quality value is derived
834 * by adding 110 to the signal level */
839 else if( signal
> -40 )
842 *buf
= (signal
+ 110);
850 int nl80211_get_quality_max(const char *ifname
, int *buf
)
852 if( wext_get_quality_max(ifname
, buf
) )
853 /* The cfg80211 wext compat layer assumes a maximum
860 int nl80211_get_encryption(const char *ifname
, char *buf
)
865 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
868 if( (res
= nl80211_hostapd_info(ifname
)) )
870 if( (val
= nl80211_getval(ifname
, res
, "auth_algs")) && (val
> 0) )
872 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
876 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
880 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
884 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
885 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
892 for( i
= 0; i
< 4; i
++ )
894 snprintf(k
, sizeof(k
), "wep_key%d", i
);
896 if( (val
= nl80211_getval(ifname
, res
, k
)) )
898 if( (strlen(val
) == 5) || (strlen(val
) == 10) )
899 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
901 else if( (strlen(val
) == 13) || (strlen(val
) == 26) )
902 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
906 c
->group_ciphers
= c
->pair_ciphers
;
912 if( (val
= nl80211_getval(ifname
, res
, "wpa")) != NULL
)
913 c
->wpa_version
= atoi(val
);
916 val
= nl80211_getval(ifname
, res
, "wpa_key_mgmt");
918 if( !val
|| strstr(val
, "PSK") )
919 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
921 if( val
&& strstr(val
, "EAP") )
922 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
924 if( val
&& strstr(val
, "NONE") )
925 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
928 if( (val
= nl80211_getval(ifname
, res
, "wpa_pairwise")) != NULL
)
930 if( strstr(val
, "TKIP") )
931 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
933 if( strstr(val
, "CCMP") )
934 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
936 if( strstr(val
, "NONE") )
937 c
->pair_ciphers
|= IWINFO_CIPHER_NONE
;
941 c
->group_ciphers
= c
->pair_ciphers
;
942 c
->enabled
= (c
->auth_algs
|| c
->auth_suites
) ? 1 : 0;
948 else if( (res
= nl80211_wpasupp_info(ifname
, "STATUS")) &&
949 (val
= nl80211_getval(NULL
, res
, "pairwise_cipher")) )
952 if( strstr(val
, "WEP") )
954 if( strstr(val
, "WEP-40") )
955 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
957 else if( strstr(val
, "WEP-104") )
958 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
961 c
->group_ciphers
= c
->pair_ciphers
;
963 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
964 c
->auth_algs
|= IWINFO_AUTH_OPEN
; /* XXX: assumption */
970 if( strstr(val
, "TKIP") )
971 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
973 else if( strstr(val
, "CCMP") )
974 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
976 else if( strstr(val
, "NONE") )
977 c
->pair_ciphers
|= IWINFO_CIPHER_NONE
;
979 else if( strstr(val
, "WEP-40") )
980 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
982 else if( strstr(val
, "WEP-104") )
983 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
986 if( (val
= nl80211_getval(NULL
, res
, "group_cipher")) )
988 if( strstr(val
, "TKIP") )
989 c
->group_ciphers
|= IWINFO_CIPHER_TKIP
;
991 else if( strstr(val
, "CCMP") )
992 c
->group_ciphers
|= IWINFO_CIPHER_CCMP
;
994 else if( strstr(val
, "NONE") )
995 c
->group_ciphers
|= IWINFO_CIPHER_NONE
;
997 else if( strstr(val
, "WEP-40") )
998 c
->group_ciphers
|= IWINFO_CIPHER_WEP40
;
1000 else if( strstr(val
, "WEP-104") )
1001 c
->group_ciphers
|= IWINFO_CIPHER_WEP104
;
1005 if( (val
= nl80211_getval(NULL
, res
, "key_mgmt")) )
1007 if( strstr(val
, "WPA2") )
1010 else if( strstr(val
, "WPA") )
1014 if( strstr(val
, "PSK") )
1015 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1017 else if( strstr(val
, "EAP") || strstr(val
, "802.1X") )
1018 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
1020 else if( strstr(val
, "NONE") )
1021 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1024 c
->enabled
= (c
->wpa_version
&& c
->auth_suites
) ? 1 : 0;
1034 static int nl80211_get_assoclist_cb(struct nl_msg
*msg
, void *arg
)
1036 struct nl80211_assoc_count
*ac
= arg
;
1037 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1038 struct nlattr
*attr
[NL80211_ATTR_MAX
+ 1];
1039 struct nlattr
*sinfo
[NL80211_STA_INFO_MAX
+ 1];
1041 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
1042 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
1043 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
1044 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
1045 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
1046 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
1047 [NL80211_STA_INFO_SIGNAL
] = { .type
= NLA_U8
},
1048 [NL80211_STA_INFO_TX_BITRATE
] = { .type
= NLA_NESTED
},
1049 [NL80211_STA_INFO_LLID
] = { .type
= NLA_U16
},
1050 [NL80211_STA_INFO_PLID
] = { .type
= NLA_U16
},
1051 [NL80211_STA_INFO_PLINK_STATE
] = { .type
= NLA_U8
},
1054 nla_parse(attr
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1055 genlmsg_attrlen(gnlh
, 0), NULL
);
1057 if( attr
[NL80211_ATTR_MAC
] )
1058 memcpy(ac
->entry
->mac
, nla_data(attr
[NL80211_ATTR_MAC
]), 6);
1060 if( attr
[NL80211_ATTR_STA_INFO
] )
1062 if( !nla_parse_nested(sinfo
, NL80211_STA_INFO_MAX
,
1063 attr
[NL80211_ATTR_STA_INFO
], stats_policy
) )
1065 if( sinfo
[NL80211_STA_INFO_SIGNAL
] )
1066 ac
->entry
->signal
= nla_get_u8(sinfo
[NL80211_STA_INFO_SIGNAL
]);
1070 ac
->entry
->noise
= ac
->noise
;
1077 int nl80211_get_assoclist(const char *ifname
, char *buf
, int *len
)
1079 struct nl80211_assoc_count ac
;
1080 struct nl80211_msg_conveyor
*req
;
1082 nl80211_get_noise(ifname
, &ac
.noise
);
1084 req
= nl80211_msg(ifname
, NL80211_CMD_GET_STATION
, NLM_F_DUMP
);
1088 ac
.entry
= (struct iwinfo_assoclist_entry
*)buf
;
1090 nl80211_cb(req
, nl80211_get_assoclist_cb
, &ac
);
1094 *len
= (ac
.count
* sizeof(struct iwinfo_assoclist_entry
));
1101 int nl80211_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
1103 int ch_cur
, ch_cmp
, bands_remain
, freqs_remain
;
1104 int dbm_max
= -1, dbm_cur
, dbm_cnt
;
1105 struct nl80211_msg_conveyor
*req
, *res
;
1106 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
1107 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1108 struct nlattr
*band
, *freq
;
1109 struct iwinfo_txpwrlist_entry entry
;
1111 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
1112 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
1113 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
1114 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
1115 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
1116 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
1117 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
1120 if( nl80211_get_channel(ifname
, &ch_cur
) )
1123 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
1126 res
= nl80211_send(req
);
1129 nla_for_each_nested(band
,
1130 res
->attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
1132 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
1133 nla_len(band
), NULL
);
1135 nla_for_each_nested(freq
,
1136 bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
1138 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
1139 nla_data(freq
), nla_len(freq
), freq_policy
);
1141 ch_cmp
= nl80211_freq2channel(
1142 nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]));
1144 if( (!ch_cur
|| (ch_cmp
== ch_cur
)) &&
1145 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] )
1147 dbm_max
= (int)(0.01 * nla_get_u32(
1148 freqs
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]));
1162 for( dbm_cur
= 0, dbm_cnt
= 0;
1164 dbm_cur
+= 2, dbm_cnt
++ )
1166 entry
.dbm
= dbm_cur
;
1167 entry
.mw
= wext_dbm2mw(dbm_cur
);
1169 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
1172 entry
.dbm
= dbm_max
;
1173 entry
.mw
= wext_dbm2mw(dbm_max
);
1175 memcpy(&buf
[dbm_cnt
* sizeof(entry
)], &entry
, sizeof(entry
));
1178 *len
= dbm_cnt
* sizeof(entry
);
1185 static void nl80211_get_scancrypto(const char *spec
,
1186 struct iwinfo_crypto_entry
*c
)
1188 if( strstr(spec
, "OPEN") )
1196 if( strstr(spec
, "WPA2-") && strstr(spec
, "WPA-") )
1199 else if( strstr(spec
, "WPA2") )
1202 else if( strstr(spec
, "WPA") )
1205 else if( strstr(spec
, "WEP") )
1206 c
->auth_algs
= IWINFO_AUTH_OPEN
| IWINFO_AUTH_SHARED
;
1209 if( strstr(spec
, "PSK") )
1210 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
1212 if( strstr(spec
, "802.1X") || strstr(spec
, "EAP") )
1213 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
1215 if( strstr(spec
, "WPA-NONE") )
1216 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
1219 if( strstr(spec
, "TKIP") )
1220 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
1222 if( strstr(spec
, "CCMP") )
1223 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
1225 if( strstr(spec
, "WEP-40") )
1226 c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
;
1228 if( strstr(spec
, "WEP-104") )
1229 c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
;
1231 c
->group_ciphers
= c
->pair_ciphers
;
1235 int nl80211_get_scanlist(const char *ifname
, char *buf
, int *len
)
1237 int freq
, rssi
, qmax
, count
;
1239 char ssid
[128] = { 0 };
1240 char bssid
[18] = { 0 };
1241 char cipher
[256] = { 0 };
1243 /* Got a radioX pseudo interface, find some interface on it or create one */
1244 if( !strncmp(ifname
, "radio", 5) )
1246 /* Reuse existing interface */
1247 if( (res
= nl80211_phy2ifname(ifname
)) != NULL
)
1249 return nl80211_get_scanlist(res
, buf
, len
);
1252 /* Need to spawn a temporary iface for scanning */
1253 else if( (res
= nl80211_ifadd(ifname
)) != NULL
)
1255 count
= nl80211_get_scanlist(res
, buf
, len
);
1261 struct iwinfo_scanlist_entry
*e
= (struct iwinfo_scanlist_entry
*)buf
;
1263 /* WPA supplicant */
1264 if( (res
= nl80211_wpasupp_info(ifname
, "SCAN")) &&
1265 !strcmp(res
, "OK\n") )
1269 if( (res
= nl80211_wpasupp_info(ifname
, "SCAN_RESULTS")) )
1271 nl80211_get_quality_max(ifname
, &qmax
);
1273 /* skip header line */
1274 while( *res
++ != '\n' );
1278 while( sscanf(res
, "%17s %d %d %255s %127[^\n]\n",
1279 bssid
, &freq
, &rssi
, cipher
, ssid
) > 0 )
1282 e
->mac
[0] = strtol(&bssid
[0], NULL
, 16);
1283 e
->mac
[1] = strtol(&bssid
[3], NULL
, 16);
1284 e
->mac
[2] = strtol(&bssid
[6], NULL
, 16);
1285 e
->mac
[3] = strtol(&bssid
[9], NULL
, 16);
1286 e
->mac
[4] = strtol(&bssid
[12], NULL
, 16);
1287 e
->mac
[5] = strtol(&bssid
[15], NULL
, 16);
1290 memcpy(e
->ssid
, ssid
,
1291 min(strlen(ssid
), sizeof(e
->ssid
) - 1));
1293 /* Mode (assume master) */
1294 sprintf((char *)e
->mode
, "Master");
1297 e
->channel
= nl80211_freq2channel(freq
);
1305 /* The cfg80211 wext compat layer assumes a signal range
1306 * of -110 dBm to -40 dBm, the quality value is derived
1307 * by adding 110 to the signal level */
1310 else if( rssi
> -40 )
1313 e
->quality
= (rssi
+ 110);
1321 e
->quality_max
= qmax
;
1324 nl80211_get_scancrypto(cipher
, &e
->crypto
);
1326 /* advance to next line */
1327 while( *res
&& *res
++ != '\n' );
1333 *len
= count
* sizeof(struct iwinfo_scanlist_entry
);
1341 /* Got a temp interface, don't create yet another one */
1342 if( !strncmp(ifname
, "tmp.", 4) )
1344 if( !nl80211_ifup(ifname
) )
1347 wext_get_scanlist(ifname
, buf
, len
);
1348 nl80211_ifdown(ifname
);
1352 /* Spawn a new scan interface */
1355 if( !(res
= nl80211_ifadd(ifname
)) )
1358 if( !nl80211_ifmac(res
) )
1361 /* if we can take the new interface up, the driver supports an
1362 * additional interface and there's no need to tear down the ap */
1363 if( nl80211_ifup(res
) )
1365 wext_get_scanlist(res
, buf
, len
);
1366 nl80211_ifdown(res
);
1369 /* driver cannot create secondary interface, take down ap
1371 else if( nl80211_ifdown(ifname
) && nl80211_ifup(res
) )
1373 wext_get_scanlist(res
, buf
, len
);
1374 nl80211_ifdown(res
);
1375 nl80211_ifup(ifname
);
1376 nl80211_hostapd_hup(ifname
);
1388 int nl80211_get_freqlist(const char *ifname
, char *buf
, int *len
)
1390 int count
= 0, bands_remain
, freqs_remain
;
1391 struct nl80211_msg_conveyor
*req
, *res
;
1392 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
1393 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1394 struct nlattr
*band
, *freq
;
1395 struct iwinfo_freqlist_entry
*e
= (struct iwinfo_freqlist_entry
*)buf
;
1397 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
1400 res
= nl80211_send(req
);
1403 nla_for_each_nested(band
,
1404 res
->attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
1406 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
1407 nla_len(band
), NULL
);
1409 nla_for_each_nested(freq
,
1410 bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
1412 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
1413 nla_data(freq
), nla_len(freq
), NULL
);
1415 if( !freqs
[NL80211_FREQUENCY_ATTR_FREQ
] ||
1416 freqs
[NL80211_FREQUENCY_ATTR_DISABLED
] )
1419 e
->mhz
= nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]);
1420 e
->channel
= nl80211_freq2channel(e
->mhz
);
1423 freqs
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] ||
1424 freqs
[NL80211_FREQUENCY_ATTR_NO_IBSS
] ||
1425 freqs
[NL80211_FREQUENCY_ATTR_RADAR
]
1439 *len
= count
* sizeof(struct iwinfo_freqlist_entry
);
1446 int nl80211_get_country(const char *ifname
, char *buf
)
1449 struct nl80211_msg_conveyor
*req
, *res
;
1451 req
= nl80211_msg(ifname
, NL80211_CMD_GET_REG
, 0);
1454 res
= nl80211_send(req
);
1457 if( res
->attr
[NL80211_ATTR_REG_ALPHA2
] )
1459 memcpy(buf
, nla_data(res
->attr
[NL80211_ATTR_REG_ALPHA2
]), 2);
1470 int nl80211_get_countrylist(const char *ifname
, char *buf
, int *len
)
1473 struct iwinfo_iso3166_label
*l
;
1474 struct iwinfo_country_entry
*e
= (struct iwinfo_country_entry
*)buf
;
1476 for( l
= ISO3166_Names
, count
= 0; l
->iso3166
; l
++, e
++, count
++ )
1478 e
->iso3166
= l
->iso3166
;
1479 e
->ccode
[0] = (l
->iso3166
/ 256);
1480 e
->ccode
[1] = (l
->iso3166
% 256);
1483 *len
= (count
* sizeof(struct iwinfo_country_entry
));
1487 int nl80211_get_hwmodelist(const char *ifname
, int *buf
)
1489 int bands_remain
, freqs_remain
;
1490 struct nl80211_msg_conveyor
*req
, *res
;
1491 struct nlattr
*bands
[NL80211_BAND_ATTR_MAX
+ 1];
1492 struct nlattr
*freqs
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1493 struct nlattr
*band
, *freq
;
1496 req
= nl80211_msg(ifname
, NL80211_CMD_GET_WIPHY
, 0);
1499 res
= nl80211_send(req
);
1502 nla_for_each_nested(band
,
1503 res
->attr
[NL80211_ATTR_WIPHY_BANDS
], bands_remain
)
1505 nla_parse(bands
, NL80211_BAND_ATTR_MAX
, nla_data(band
),
1506 nla_len(band
), NULL
);
1508 if( bands
[NL80211_BAND_ATTR_HT_CAPA
] )
1509 caps
= nla_get_u16(bands
[NL80211_BAND_ATTR_HT_CAPA
]);
1511 /* Treat HT20/HT40 as 11n */
1512 if( caps
& (1 << 1) )
1513 *buf
|= IWINFO_80211_N
;
1515 nla_for_each_nested(freq
,
1516 bands
[NL80211_BAND_ATTR_FREQS
], freqs_remain
)
1518 nla_parse(freqs
, NL80211_FREQUENCY_ATTR_MAX
,
1519 nla_data(freq
), nla_len(freq
), NULL
);
1521 if( !freqs
[NL80211_FREQUENCY_ATTR_FREQ
] )
1524 if( nla_get_u32(freqs
[NL80211_FREQUENCY_ATTR_FREQ
]) < 2485 )
1526 *buf
|= IWINFO_80211_B
;
1527 *buf
|= IWINFO_80211_G
;
1531 *buf
|= IWINFO_80211_A
;
1540 return *buf
? 0 : -1;
1543 int nl80211_get_mbssid_support(const char *ifname
, int *buf
)
1545 /* We assume that multi bssid is always possible */