2 * WPA Supplicant - driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <sys/ioctl.h>
17 #include <net/if_arp.h>
18 #include <netlink/genl/genl.h>
19 #include <netlink/genl/family.h>
20 #include <netlink/genl/ctrl.h>
21 #include <linux/nl80211.h>
23 #include "wireless_copy.h"
27 #include "ieee802_11_defs.h"
30 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
33 #define IFF_DORMANT 0x20000 /* driver signals dormant */
36 #ifndef IF_OPER_DORMANT
37 #define IF_OPER_DORMANT 5
44 struct wpa_driver_nl80211_data
{
48 char ifname
[IFNAMSIZ
+ 1];
51 size_t assoc_req_ies_len
;
53 size_t assoc_resp_ies_len
;
54 struct wpa_driver_capa capa
;
56 int we_version_compiled
;
58 /* for set_auth_alg fallback */
60 int auth_alg_fallback
;
64 char mlmedev
[IFNAMSIZ
+ 1];
66 int scan_complete_events
;
68 struct nl_handle
*nl_handle
;
69 struct nl_cache
*nl_cache
;
71 struct genl_family
*nl80211
;
75 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
,
77 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
);
78 static int wpa_driver_nl80211_flush_pmkid(void *priv
);
79 static int wpa_driver_nl80211_get_range(void *priv
);
81 static int wpa_driver_nl80211_send_oper_ifla(
82 struct wpa_driver_nl80211_data
*drv
,
83 int linkmode
, int operstate
)
87 struct ifinfomsg ifinfo
;
94 os_memset(&req
, 0, sizeof(req
));
96 req
.hdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
97 req
.hdr
.nlmsg_type
= RTM_SETLINK
;
98 req
.hdr
.nlmsg_flags
= NLM_F_REQUEST
;
99 req
.hdr
.nlmsg_seq
= ++nl_seq
;
100 req
.hdr
.nlmsg_pid
= 0;
102 req
.ifinfo
.ifi_family
= AF_UNSPEC
;
103 req
.ifinfo
.ifi_type
= 0;
104 req
.ifinfo
.ifi_index
= drv
->ifindex
;
105 req
.ifinfo
.ifi_flags
= 0;
106 req
.ifinfo
.ifi_change
= 0;
108 if (linkmode
!= -1) {
109 rta
= (struct rtattr
*)
110 ((char *) &req
+ NLMSG_ALIGN(req
.hdr
.nlmsg_len
));
111 rta
->rta_type
= IFLA_LINKMODE
;
112 rta
->rta_len
= RTA_LENGTH(sizeof(char));
113 *((char *) RTA_DATA(rta
)) = linkmode
;
114 req
.hdr
.nlmsg_len
= NLMSG_ALIGN(req
.hdr
.nlmsg_len
) +
115 RTA_LENGTH(sizeof(char));
117 if (operstate
!= -1) {
118 rta
= (struct rtattr
*)
119 ((char *) &req
+ NLMSG_ALIGN(req
.hdr
.nlmsg_len
));
120 rta
->rta_type
= IFLA_OPERSTATE
;
121 rta
->rta_len
= RTA_LENGTH(sizeof(char));
122 *((char *) RTA_DATA(rta
)) = operstate
;
123 req
.hdr
.nlmsg_len
= NLMSG_ALIGN(req
.hdr
.nlmsg_len
) +
124 RTA_LENGTH(sizeof(char));
127 wpa_printf(MSG_DEBUG
, "WEXT: Operstate: linkmode=%d, operstate=%d",
128 linkmode
, operstate
);
130 ret
= send(drv
->event_sock
, &req
, req
.hdr
.nlmsg_len
, 0);
132 wpa_printf(MSG_DEBUG
, "WEXT: Sending operstate IFLA failed: "
133 "%s (assume operstate is not supported)",
137 return ret
< 0 ? -1 : 0;
141 static int wpa_driver_nl80211_set_auth_param(
142 struct wpa_driver_nl80211_data
*drv
, int idx
, u32 value
)
147 os_memset(&iwr
, 0, sizeof(iwr
));
148 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
149 iwr
.u
.param
.flags
= idx
& IW_AUTH_INDEX
;
150 iwr
.u
.param
.value
= value
;
152 if (ioctl(drv
->ioctl_sock
, SIOCSIWAUTH
, &iwr
) < 0) {
153 if (errno
!= EOPNOTSUPP
) {
154 wpa_printf(MSG_DEBUG
, "WEXT: SIOCSIWAUTH(param %d "
155 "value 0x%x) failed: %s)",
156 idx
, value
, strerror(errno
));
158 ret
= errno
== EOPNOTSUPP
? -2 : -1;
165 static int wpa_driver_nl80211_get_bssid(void *priv
, u8
*bssid
)
167 struct wpa_driver_nl80211_data
*drv
= priv
;
171 os_memset(&iwr
, 0, sizeof(iwr
));
172 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
174 if (ioctl(drv
->ioctl_sock
, SIOCGIWAP
, &iwr
) < 0) {
175 perror("ioctl[SIOCGIWAP]");
178 os_memcpy(bssid
, iwr
.u
.ap_addr
.sa_data
, ETH_ALEN
);
184 static int wpa_driver_nl80211_set_bssid(void *priv
, const u8
*bssid
)
186 struct wpa_driver_nl80211_data
*drv
= priv
;
190 os_memset(&iwr
, 0, sizeof(iwr
));
191 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
192 iwr
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
194 os_memcpy(iwr
.u
.ap_addr
.sa_data
, bssid
, ETH_ALEN
);
196 os_memset(iwr
.u
.ap_addr
.sa_data
, 0, ETH_ALEN
);
198 if (ioctl(drv
->ioctl_sock
, SIOCSIWAP
, &iwr
) < 0) {
199 perror("ioctl[SIOCSIWAP]");
207 static int wpa_driver_nl80211_get_ssid(void *priv
, u8
*ssid
)
209 struct wpa_driver_nl80211_data
*drv
= priv
;
213 os_memset(&iwr
, 0, sizeof(iwr
));
214 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
215 iwr
.u
.essid
.pointer
= (caddr_t
) ssid
;
216 iwr
.u
.essid
.length
= 32;
218 if (ioctl(drv
->ioctl_sock
, SIOCGIWESSID
, &iwr
) < 0) {
219 perror("ioctl[SIOCGIWESSID]");
222 ret
= iwr
.u
.essid
.length
;
225 /* Some drivers include nul termination in the SSID, so let's
226 * remove it here before further processing. WE-21 changes this
227 * to explicitly require the length _not_ to include nul
229 if (ret
> 0 && ssid
[ret
- 1] == '\0' &&
230 drv
->we_version_compiled
< 21)
238 static int wpa_driver_nl80211_set_ssid(void *priv
, const u8
*ssid
,
241 struct wpa_driver_nl80211_data
*drv
= priv
;
249 os_memset(&iwr
, 0, sizeof(iwr
));
250 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
251 /* flags: 1 = ESSID is active, 0 = not (promiscuous) */
252 iwr
.u
.essid
.flags
= (ssid_len
!= 0);
253 os_memset(buf
, 0, sizeof(buf
));
254 os_memcpy(buf
, ssid
, ssid_len
);
255 iwr
.u
.essid
.pointer
= (caddr_t
) buf
;
256 if (drv
->we_version_compiled
< 21) {
257 /* For historic reasons, set SSID length to include one extra
258 * character, C string nul termination, even though SSID is
259 * really an octet string that should not be presented as a C
260 * string. Some Linux drivers decrement the length by one and
261 * can thus end up missing the last octet of the SSID if the
262 * length is not incremented here. WE-21 changes this to
263 * explicitly require the length _not_ to include nul
268 iwr
.u
.essid
.length
= ssid_len
;
270 if (ioctl(drv
->ioctl_sock
, SIOCSIWESSID
, &iwr
) < 0) {
271 perror("ioctl[SIOCSIWESSID]");
279 static int wpa_driver_nl80211_set_freq(void *priv
, int freq
)
281 struct wpa_driver_nl80211_data
*drv
= priv
;
285 os_memset(&iwr
, 0, sizeof(iwr
));
286 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
287 iwr
.u
.freq
.m
= freq
* 100000;
290 if (ioctl(drv
->ioctl_sock
, SIOCSIWFREQ
, &iwr
) < 0) {
291 perror("ioctl[SIOCSIWFREQ]");
300 wpa_driver_nl80211_event_wireless_custom(void *ctx
, char *custom
)
302 union wpa_event_data data
;
304 wpa_printf(MSG_MSGDUMP
, "WEXT: Custom wireless event: '%s'",
307 os_memset(&data
, 0, sizeof(data
));
309 if (os_strncmp(custom
, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
310 data
.michael_mic_failure
.unicast
=
311 os_strstr(custom
, " unicast ") != NULL
;
312 /* TODO: parse parameters(?) */
313 wpa_supplicant_event(ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
314 } else if (os_strncmp(custom
, "ASSOCINFO(ReqIEs=", 17) == 0) {
320 bytes
= strspn(spos
, "0123456789abcdefABCDEF");
321 if (!bytes
|| (bytes
& 1))
325 data
.assoc_info
.req_ies
= os_malloc(bytes
);
326 if (data
.assoc_info
.req_ies
== NULL
)
329 data
.assoc_info
.req_ies_len
= bytes
;
330 hexstr2bin(spos
, data
.assoc_info
.req_ies
, bytes
);
334 data
.assoc_info
.resp_ies
= NULL
;
335 data
.assoc_info
.resp_ies_len
= 0;
337 if (os_strncmp(spos
, " RespIEs=", 9) == 0) {
340 bytes
= strspn(spos
, "0123456789abcdefABCDEF");
341 if (!bytes
|| (bytes
& 1))
345 data
.assoc_info
.resp_ies
= os_malloc(bytes
);
346 if (data
.assoc_info
.resp_ies
== NULL
)
349 data
.assoc_info
.resp_ies_len
= bytes
;
350 hexstr2bin(spos
, data
.assoc_info
.resp_ies
, bytes
);
353 wpa_supplicant_event(ctx
, EVENT_ASSOCINFO
, &data
);
356 os_free(data
.assoc_info
.resp_ies
);
357 os_free(data
.assoc_info
.req_ies
);
358 #ifdef CONFIG_PEERKEY
359 } else if (os_strncmp(custom
, "STKSTART.request=", 17) == 0) {
360 if (hwaddr_aton(custom
+ 17, data
.stkstart
.peer
)) {
361 wpa_printf(MSG_DEBUG
, "WEXT: unrecognized "
362 "STKSTART.request '%s'", custom
+ 17);
365 wpa_supplicant_event(ctx
, EVENT_STKSTART
, &data
);
366 #endif /* CONFIG_PEERKEY */
371 static int wpa_driver_nl80211_event_wireless_michaelmicfailure(
372 void *ctx
, const char *ev
, size_t len
)
374 const struct iw_michaelmicfailure
*mic
;
375 union wpa_event_data data
;
377 if (len
< sizeof(*mic
))
380 mic
= (const struct iw_michaelmicfailure
*) ev
;
382 wpa_printf(MSG_DEBUG
, "Michael MIC failure wireless event: "
383 "flags=0x%x src_addr=" MACSTR
, mic
->flags
,
384 MAC2STR(mic
->src_addr
.sa_data
));
386 os_memset(&data
, 0, sizeof(data
));
387 data
.michael_mic_failure
.unicast
= !(mic
->flags
& IW_MICFAILURE_GROUP
);
388 wpa_supplicant_event(ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
394 static int wpa_driver_nl80211_event_wireless_pmkidcand(
395 struct wpa_driver_nl80211_data
*drv
, const char *ev
, size_t len
)
397 const struct iw_pmkid_cand
*cand
;
398 union wpa_event_data data
;
401 if (len
< sizeof(*cand
))
404 cand
= (const struct iw_pmkid_cand
*) ev
;
405 addr
= (const u8
*) cand
->bssid
.sa_data
;
407 wpa_printf(MSG_DEBUG
, "PMKID candidate wireless event: "
408 "flags=0x%x index=%d bssid=" MACSTR
, cand
->flags
,
409 cand
->index
, MAC2STR(addr
));
411 os_memset(&data
, 0, sizeof(data
));
412 os_memcpy(data
.pmkid_candidate
.bssid
, addr
, ETH_ALEN
);
413 data
.pmkid_candidate
.index
= cand
->index
;
414 data
.pmkid_candidate
.preauth
= cand
->flags
& IW_PMKID_CAND_PREAUTH
;
415 wpa_supplicant_event(drv
->ctx
, EVENT_PMKID_CANDIDATE
, &data
);
421 static int wpa_driver_nl80211_event_wireless_assocreqie(
422 struct wpa_driver_nl80211_data
*drv
, const char *ev
, int len
)
427 wpa_hexdump(MSG_DEBUG
, "AssocReq IE wireless event", (const u8
*) ev
,
429 os_free(drv
->assoc_req_ies
);
430 drv
->assoc_req_ies
= os_malloc(len
);
431 if (drv
->assoc_req_ies
== NULL
) {
432 drv
->assoc_req_ies_len
= 0;
435 os_memcpy(drv
->assoc_req_ies
, ev
, len
);
436 drv
->assoc_req_ies_len
= len
;
442 static int wpa_driver_nl80211_event_wireless_assocrespie(
443 struct wpa_driver_nl80211_data
*drv
, const char *ev
, int len
)
448 wpa_hexdump(MSG_DEBUG
, "AssocResp IE wireless event", (const u8
*) ev
,
450 os_free(drv
->assoc_resp_ies
);
451 drv
->assoc_resp_ies
= os_malloc(len
);
452 if (drv
->assoc_resp_ies
== NULL
) {
453 drv
->assoc_resp_ies_len
= 0;
456 os_memcpy(drv
->assoc_resp_ies
, ev
, len
);
457 drv
->assoc_resp_ies_len
= len
;
463 static void wpa_driver_nl80211_event_assoc_ies(struct wpa_driver_nl80211_data
*drv
)
465 union wpa_event_data data
;
467 if (drv
->assoc_req_ies
== NULL
&& drv
->assoc_resp_ies
== NULL
)
470 os_memset(&data
, 0, sizeof(data
));
471 if (drv
->assoc_req_ies
) {
472 data
.assoc_info
.req_ies
= drv
->assoc_req_ies
;
473 drv
->assoc_req_ies
= NULL
;
474 data
.assoc_info
.req_ies_len
= drv
->assoc_req_ies_len
;
476 if (drv
->assoc_resp_ies
) {
477 data
.assoc_info
.resp_ies
= drv
->assoc_resp_ies
;
478 drv
->assoc_resp_ies
= NULL
;
479 data
.assoc_info
.resp_ies_len
= drv
->assoc_resp_ies_len
;
482 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOCINFO
, &data
);
484 os_free(data
.assoc_info
.req_ies
);
485 os_free(data
.assoc_info
.resp_ies
);
489 static void wpa_driver_nl80211_event_wireless(struct wpa_driver_nl80211_data
*drv
,
490 void *ctx
, char *data
, int len
)
492 struct iw_event iwe_buf
, *iwe
= &iwe_buf
;
493 char *pos
, *end
, *custom
, *buf
;
498 while (pos
+ IW_EV_LCP_LEN
<= end
) {
499 /* Event data may be unaligned, so make a local, aligned copy
500 * before processing. */
501 os_memcpy(&iwe_buf
, pos
, IW_EV_LCP_LEN
);
502 wpa_printf(MSG_DEBUG
, "Wireless event: cmd=0x%x len=%d",
504 if (iwe
->len
<= IW_EV_LCP_LEN
)
507 custom
= pos
+ IW_EV_POINT_LEN
;
508 if (drv
->we_version_compiled
> 18 &&
509 (iwe
->cmd
== IWEVMICHAELMICFAILURE
||
510 iwe
->cmd
== IWEVCUSTOM
||
511 iwe
->cmd
== IWEVASSOCREQIE
||
512 iwe
->cmd
== IWEVASSOCRESPIE
||
513 iwe
->cmd
== IWEVPMKIDCAND
)) {
514 /* WE-19 removed the pointer from struct iw_point */
515 char *dpos
= (char *) &iwe_buf
.u
.data
.length
;
516 int dlen
= dpos
- (char *) &iwe_buf
;
517 os_memcpy(dpos
, pos
+ IW_EV_LCP_LEN
,
518 sizeof(struct iw_event
) - dlen
);
520 os_memcpy(&iwe_buf
, pos
, sizeof(struct iw_event
));
521 custom
+= IW_EV_POINT_OFF
;
526 wpa_printf(MSG_DEBUG
, "Wireless event: new AP: "
528 MAC2STR((u8
*) iwe
->u
.ap_addr
.sa_data
));
529 if (is_zero_ether_addr(
530 (const u8
*) iwe
->u
.ap_addr
.sa_data
) ||
531 os_memcmp(iwe
->u
.ap_addr
.sa_data
,
532 "\x44\x44\x44\x44\x44\x44", ETH_ALEN
) ==
534 os_free(drv
->assoc_req_ies
);
535 drv
->assoc_req_ies
= NULL
;
536 os_free(drv
->assoc_resp_ies
);
537 drv
->assoc_resp_ies
= NULL
;
538 wpa_supplicant_event(ctx
, EVENT_DISASSOC
,
542 wpa_driver_nl80211_event_assoc_ies(drv
);
543 wpa_supplicant_event(ctx
, EVENT_ASSOC
, NULL
);
546 case IWEVMICHAELMICFAILURE
:
547 wpa_driver_nl80211_event_wireless_michaelmicfailure(
548 ctx
, custom
, iwe
->u
.data
.length
);
551 if (custom
+ iwe
->u
.data
.length
> end
)
553 buf
= os_malloc(iwe
->u
.data
.length
+ 1);
556 os_memcpy(buf
, custom
, iwe
->u
.data
.length
);
557 buf
[iwe
->u
.data
.length
] = '\0';
558 wpa_driver_nl80211_event_wireless_custom(ctx
, buf
);
562 drv
->scan_complete_events
= 1;
563 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
,
565 wpa_supplicant_event(ctx
, EVENT_SCAN_RESULTS
, NULL
);
568 wpa_driver_nl80211_event_wireless_assocreqie(
569 drv
, custom
, iwe
->u
.data
.length
);
571 case IWEVASSOCRESPIE
:
572 wpa_driver_nl80211_event_wireless_assocrespie(
573 drv
, custom
, iwe
->u
.data
.length
);
576 wpa_driver_nl80211_event_wireless_pmkidcand(
577 drv
, custom
, iwe
->u
.data
.length
);
586 static void wpa_driver_nl80211_event_link(void *ctx
, char *buf
, size_t len
,
589 union wpa_event_data event
;
591 os_memset(&event
, 0, sizeof(event
));
592 if (len
> sizeof(event
.interface_status
.ifname
))
593 len
= sizeof(event
.interface_status
.ifname
) - 1;
594 os_memcpy(event
.interface_status
.ifname
, buf
, len
);
595 event
.interface_status
.ievent
= del
? EVENT_INTERFACE_REMOVED
:
596 EVENT_INTERFACE_ADDED
;
598 wpa_printf(MSG_DEBUG
, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
600 event
.interface_status
.ifname
,
601 del
? "removed" : "added");
603 wpa_supplicant_event(ctx
, EVENT_INTERFACE_STATUS
, &event
);
607 static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data
*drv
,
608 void *ctx
, struct nlmsghdr
*h
,
611 struct ifinfomsg
*ifi
;
612 int attrlen
, _nlmsg_len
, rta_len
;
613 struct rtattr
* attr
;
615 if (len
< sizeof(*ifi
))
620 if (drv
->ifindex
!= ifi
->ifi_index
) {
621 wpa_printf(MSG_DEBUG
, "Ignore event for foreign ifindex %d",
626 wpa_printf(MSG_DEBUG
, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
628 drv
->operstate
, ifi
->ifi_flags
,
629 (ifi
->ifi_flags
& IFF_UP
) ? "[UP]" : "",
630 (ifi
->ifi_flags
& IFF_RUNNING
) ? "[RUNNING]" : "",
631 (ifi
->ifi_flags
& IFF_LOWER_UP
) ? "[LOWER_UP]" : "",
632 (ifi
->ifi_flags
& IFF_DORMANT
) ? "[DORMANT]" : "");
634 * Some drivers send the association event before the operup event--in
635 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
636 * fails. This will hit us when wpa_supplicant does not need to do
637 * IEEE 802.1X authentication
639 if (drv
->operstate
== 1 &&
640 (ifi
->ifi_flags
& (IFF_LOWER_UP
| IFF_DORMANT
)) == IFF_LOWER_UP
&&
641 !(ifi
->ifi_flags
& IFF_RUNNING
))
642 wpa_driver_nl80211_send_oper_ifla(drv
, -1, IF_OPER_UP
);
644 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
646 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
650 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
652 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
653 while (RTA_OK(attr
, attrlen
)) {
654 if (attr
->rta_type
== IFLA_WIRELESS
) {
655 wpa_driver_nl80211_event_wireless(
656 drv
, ctx
, ((char *) attr
) + rta_len
,
657 attr
->rta_len
- rta_len
);
658 } else if (attr
->rta_type
== IFLA_IFNAME
) {
659 wpa_driver_nl80211_event_link(ctx
,
660 ((char *) attr
) + rta_len
,
661 attr
->rta_len
- rta_len
, 0);
663 attr
= RTA_NEXT(attr
, attrlen
);
668 static void wpa_driver_nl80211_event_rtm_dellink(struct wpa_driver_nl80211_data
*drv
,
669 void *ctx
, struct nlmsghdr
*h
,
672 struct ifinfomsg
*ifi
;
673 int attrlen
, _nlmsg_len
, rta_len
;
674 struct rtattr
* attr
;
676 if (len
< sizeof(*ifi
))
681 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
683 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
687 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
689 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
690 while (RTA_OK(attr
, attrlen
)) {
691 if (attr
->rta_type
== IFLA_IFNAME
) {
692 wpa_driver_nl80211_event_link(ctx
,
693 ((char *) attr
) + rta_len
,
694 attr
->rta_len
- rta_len
, 1);
696 attr
= RTA_NEXT(attr
, attrlen
);
701 static void wpa_driver_nl80211_event_receive(int sock
, void *eloop_ctx
,
706 struct sockaddr_nl from
;
712 fromlen
= sizeof(from
);
713 left
= recvfrom(sock
, buf
, sizeof(buf
), MSG_DONTWAIT
,
714 (struct sockaddr
*) &from
, &fromlen
);
716 if (errno
!= EINTR
&& errno
!= EAGAIN
)
717 perror("recvfrom(netlink)");
721 h
= (struct nlmsghdr
*) buf
;
722 while (left
>= (int) sizeof(*h
)) {
726 plen
= len
- sizeof(*h
);
727 if (len
> left
|| plen
< 0) {
728 wpa_printf(MSG_DEBUG
, "Malformed netlink message: "
729 "len=%d left=%d plen=%d",
734 switch (h
->nlmsg_type
) {
736 wpa_driver_nl80211_event_rtm_newlink(eloop_ctx
, sock_ctx
,
740 wpa_driver_nl80211_event_rtm_dellink(eloop_ctx
, sock_ctx
,
745 len
= NLMSG_ALIGN(len
);
747 h
= (struct nlmsghdr
*) ((char *) h
+ len
);
751 wpa_printf(MSG_DEBUG
, "%d extra bytes in the end of netlink "
755 if (--max_events
> 0) {
757 * Try to receive all events in one eloop call in order to
758 * limit race condition on cases where AssocInfo event, Assoc
759 * event, and EAPOL frames are received more or less at the
760 * same time. We want to process the event messages first
761 * before starting EAPOL processing.
768 static int wpa_driver_nl80211_get_ifflags_ifname(struct wpa_driver_nl80211_data
*drv
,
769 const char *ifname
, int *flags
)
773 os_memset(&ifr
, 0, sizeof(ifr
));
774 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
775 if (ioctl(drv
->ioctl_sock
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
776 perror("ioctl[SIOCGIFFLAGS]");
779 *flags
= ifr
.ifr_flags
& 0xffff;
785 * wpa_driver_nl80211_get_ifflags - Get interface flags (SIOCGIFFLAGS)
786 * @drv: driver_nl80211 private data
787 * @flags: Pointer to returned flags value
788 * Returns: 0 on success, -1 on failure
790 static int wpa_driver_nl80211_get_ifflags(struct wpa_driver_nl80211_data
*drv
,
793 return wpa_driver_nl80211_get_ifflags_ifname(drv
, drv
->ifname
, flags
);
797 static int wpa_driver_nl80211_set_ifflags_ifname(
798 struct wpa_driver_nl80211_data
*drv
,
799 const char *ifname
, int flags
)
803 os_memset(&ifr
, 0, sizeof(ifr
));
804 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
805 ifr
.ifr_flags
= flags
& 0xffff;
806 if (ioctl(drv
->ioctl_sock
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
807 perror("SIOCSIFFLAGS");
815 * wpa_driver_nl80211_set_ifflags - Set interface flags (SIOCSIFFLAGS)
816 * @drv: driver_nl80211 private data
817 * @flags: New value for flags
818 * Returns: 0 on success, -1 on failure
820 static int wpa_driver_nl80211_set_ifflags(struct wpa_driver_nl80211_data
*drv
,
823 return wpa_driver_nl80211_set_ifflags_ifname(drv
, drv
->ifname
, flags
);
828 * wpa_driver_nl80211_init - Initialize WE driver interface
829 * @ctx: context to be used when calling wpa_supplicant functions,
830 * e.g., wpa_supplicant_event()
831 * @ifname: interface name, e.g., wlan0
832 * Returns: Pointer to private data, %NULL on failure
834 void * wpa_driver_nl80211_init(void *ctx
, const char *ifname
)
837 struct sockaddr_nl local
;
838 struct wpa_driver_nl80211_data
*drv
;
840 drv
= os_zalloc(sizeof(*drv
));
844 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
846 drv
->nl_cb
= nl_cb_alloc(NL_CB_DEFAULT
);
847 if (drv
->nl_cb
== NULL
) {
848 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
853 drv
->nl_handle
= nl_handle_alloc_cb(drv
->nl_cb
);
854 if (drv
->nl_handle
== NULL
) {
855 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
860 if (genl_connect(drv
->nl_handle
)) {
861 wpa_printf(MSG_ERROR
, "nl80211: Failed to connect to generic "
866 drv
->nl_cache
= genl_ctrl_alloc_cache(drv
->nl_handle
);
867 if (drv
->nl_cache
== NULL
) {
868 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
873 drv
->nl80211
= genl_ctrl_search_by_name(drv
->nl_cache
, "nl80211");
874 if (drv
->nl80211
== NULL
) {
875 wpa_printf(MSG_ERROR
, "nl80211: 'nl80211' generic netlink not "
880 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
881 if (drv
->ioctl_sock
< 0) {
882 perror("socket(PF_INET,SOCK_DGRAM)");
886 s
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
888 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
892 os_memset(&local
, 0, sizeof(local
));
893 local
.nl_family
= AF_NETLINK
;
894 local
.nl_groups
= RTMGRP_LINK
;
895 if (bind(s
, (struct sockaddr
*) &local
, sizeof(local
)) < 0) {
896 perror("bind(netlink)");
901 eloop_register_read_sock(s
, wpa_driver_nl80211_event_receive
, drv
,
905 if (wpa_driver_nl80211_get_ifflags(drv
, &flags
) != 0)
906 printf("Could not get interface '%s' flags\n", drv
->ifname
);
907 else if (!(flags
& IFF_UP
)) {
908 if (wpa_driver_nl80211_set_ifflags(drv
, flags
| IFF_UP
) != 0) {
909 printf("Could not set interface '%s' UP\n",
913 * Wait some time to allow driver to initialize before
914 * starting configuring the driver. This seems to be
915 * needed at least some drivers that load firmware etc.
916 * when the interface is set up.
918 wpa_printf(MSG_DEBUG
, "Interface %s set UP - waiting "
919 "a second for the driver to complete "
920 "initialization", drv
->ifname
);
926 * Make sure that the driver does not have any obsolete PMKID entries.
928 wpa_driver_nl80211_flush_pmkid(drv
);
930 if (wpa_driver_nl80211_set_mode(drv
, 0) < 0) {
931 printf("Could not configure driver to use managed mode\n");
934 wpa_driver_nl80211_get_range(drv
);
936 drv
->ifindex
= if_nametoindex(drv
->ifname
);
938 wpa_driver_nl80211_send_oper_ifla(drv
, 1, IF_OPER_DORMANT
);
943 close(drv
->ioctl_sock
);
945 genl_family_put(drv
->nl80211
);
947 nl_cache_free(drv
->nl_cache
);
949 nl_handle_destroy(drv
->nl_handle
);
951 nl_cb_put(drv
->nl_cb
);
959 * wpa_driver_nl80211_deinit - Deinitialize WE driver interface
960 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
962 * Shut down driver interface and processing of driver events. Free
963 * private data buffer if one was allocated in wpa_driver_nl80211_init().
965 void wpa_driver_nl80211_deinit(void *priv
)
967 struct wpa_driver_nl80211_data
*drv
= priv
;
970 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
973 * Clear possibly configured driver parameters in order to make it
974 * easier to use the driver after wpa_supplicant has been terminated.
976 (void) wpa_driver_nl80211_set_bssid(drv
,
977 (u8
*) "\x00\x00\x00\x00\x00\x00");
979 wpa_driver_nl80211_send_oper_ifla(priv
, 0, IF_OPER_UP
);
981 eloop_unregister_read_sock(drv
->event_sock
);
983 if (wpa_driver_nl80211_get_ifflags(drv
, &flags
) == 0)
984 (void) wpa_driver_nl80211_set_ifflags(drv
, flags
& ~IFF_UP
);
986 close(drv
->event_sock
);
987 close(drv
->ioctl_sock
);
988 os_free(drv
->assoc_req_ies
);
989 os_free(drv
->assoc_resp_ies
);
991 genl_family_put(drv
->nl80211
);
992 nl_cache_free(drv
->nl_cache
);
993 nl_handle_destroy(drv
->nl_handle
);
994 nl_cb_put(drv
->nl_cb
);
1001 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
1002 * @eloop_ctx: Unused
1003 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1005 * This function can be used as registered timeout when starting a scan to
1006 * generate a scan completed event if the driver does not report this.
1008 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
1010 wpa_printf(MSG_DEBUG
, "Scan timeout - try to get results");
1011 wpa_supplicant_event(timeout_ctx
, EVENT_SCAN_RESULTS
, NULL
);
1016 * wpa_driver_nl80211_scan - Request the driver to initiate scan
1017 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1018 * @ssid: Specific SSID to scan for (ProbeReq) or %NULL to scan for
1019 * all SSIDs (either active scan with broadcast SSID or passive
1021 * @ssid_len: Length of the SSID
1022 * Returns: 0 on success, -1 on failure
1024 static int wpa_driver_nl80211_scan(void *priv
, const u8
*ssid
, size_t ssid_len
)
1026 struct wpa_driver_nl80211_data
*drv
= priv
;
1028 int ret
= 0, timeout
;
1029 struct iw_scan_req req
;
1031 if (ssid_len
> IW_ESSID_MAX_SIZE
) {
1032 wpa_printf(MSG_DEBUG
, "%s: too long SSID (%lu)",
1033 __FUNCTION__
, (unsigned long) ssid_len
);
1037 os_memset(&iwr
, 0, sizeof(iwr
));
1038 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1040 if (ssid
&& ssid_len
) {
1041 os_memset(&req
, 0, sizeof(req
));
1042 req
.essid_len
= ssid_len
;
1043 req
.bssid
.sa_family
= ARPHRD_ETHER
;
1044 os_memset(req
.bssid
.sa_data
, 0xff, ETH_ALEN
);
1045 os_memcpy(req
.essid
, ssid
, ssid_len
);
1046 iwr
.u
.data
.pointer
= (caddr_t
) &req
;
1047 iwr
.u
.data
.length
= sizeof(req
);
1048 iwr
.u
.data
.flags
= IW_SCAN_THIS_ESSID
;
1051 if (ioctl(drv
->ioctl_sock
, SIOCSIWSCAN
, &iwr
) < 0) {
1052 perror("ioctl[SIOCSIWSCAN]");
1056 /* Not all drivers generate "scan completed" wireless event, so try to
1057 * read results after a timeout. */
1059 if (drv
->scan_complete_events
) {
1061 * The driver seems to deliver SIOCGIWSCAN events to notify
1062 * when scan is complete, so use longer timeout to avoid race
1063 * conditions with scanning and following association request.
1067 wpa_printf(MSG_DEBUG
, "Scan requested (ret=%d) - scan timeout %d "
1068 "seconds", ret
, timeout
);
1069 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1070 eloop_register_timeout(timeout
, 0, wpa_driver_nl80211_scan_timeout
, drv
,
1077 static u8
* wpa_driver_nl80211_giwscan(struct wpa_driver_nl80211_data
*drv
,
1084 res_buf_len
= IW_SCAN_MAX_DATA
;
1086 res_buf
= os_malloc(res_buf_len
);
1087 if (res_buf
== NULL
)
1089 os_memset(&iwr
, 0, sizeof(iwr
));
1090 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1091 iwr
.u
.data
.pointer
= res_buf
;
1092 iwr
.u
.data
.length
= res_buf_len
;
1094 if (ioctl(drv
->ioctl_sock
, SIOCGIWSCAN
, &iwr
) == 0)
1097 if (errno
== E2BIG
&& res_buf_len
< 100000) {
1101 wpa_printf(MSG_DEBUG
, "Scan results did not fit - "
1102 "trying larger buffer (%lu bytes)",
1103 (unsigned long) res_buf_len
);
1105 perror("ioctl[SIOCGIWSCAN]");
1111 if (iwr
.u
.data
.length
> res_buf_len
) {
1115 *len
= iwr
.u
.data
.length
;
1122 * Data structure for collecting WEXT scan results. This is needed to allow
1123 * the various methods of reporting IEs to be combined into a single IE buffer.
1125 struct wext_scan_data
{
1126 struct wpa_scan_res res
;
1135 static void wext_get_scan_mode(struct iw_event
*iwe
,
1136 struct wext_scan_data
*res
)
1138 if (iwe
->u
.mode
== IW_MODE_ADHOC
)
1139 res
->res
.caps
|= IEEE80211_CAP_IBSS
;
1140 else if (iwe
->u
.mode
== IW_MODE_MASTER
|| iwe
->u
.mode
== IW_MODE_INFRA
)
1141 res
->res
.caps
|= IEEE80211_CAP_ESS
;
1145 static void wext_get_scan_ssid(struct iw_event
*iwe
,
1146 struct wext_scan_data
*res
, char *custom
,
1149 int ssid_len
= iwe
->u
.essid
.length
;
1150 if (custom
+ ssid_len
> end
)
1152 if (iwe
->u
.essid
.flags
&&
1154 ssid_len
<= IW_ESSID_MAX_SIZE
) {
1155 os_memcpy(res
->ssid
, custom
, ssid_len
);
1156 res
->ssid_len
= ssid_len
;
1161 static void wext_get_scan_freq(struct iw_event
*iwe
,
1162 struct wext_scan_data
*res
)
1164 int divi
= 1000000, i
;
1166 if (iwe
->u
.freq
.e
== 0) {
1168 * Some drivers do not report frequency, but a channel.
1169 * Try to map this to frequency by assuming they are using
1170 * IEEE 802.11b/g. But don't overwrite a previously parsed
1171 * frequency if the driver sends both frequency and channel,
1172 * since the driver may be sending an A-band channel that we
1173 * don't handle here.
1179 if (iwe
->u
.freq
.m
>= 1 && iwe
->u
.freq
.m
<= 13) {
1180 res
->res
.freq
= 2407 + 5 * iwe
->u
.freq
.m
;
1182 } else if (iwe
->u
.freq
.m
== 14) {
1183 res
->res
.freq
= 2484;
1188 if (iwe
->u
.freq
.e
> 6) {
1189 wpa_printf(MSG_DEBUG
, "Invalid freq in scan results (BSSID="
1190 MACSTR
" m=%d e=%d)",
1191 MAC2STR(res
->res
.bssid
), iwe
->u
.freq
.m
,
1196 for (i
= 0; i
< iwe
->u
.freq
.e
; i
++)
1198 res
->res
.freq
= iwe
->u
.freq
.m
/ divi
;
1202 static void wext_get_scan_qual(struct iw_event
*iwe
,
1203 struct wext_scan_data
*res
)
1205 res
->res
.qual
= iwe
->u
.qual
.qual
;
1206 res
->res
.noise
= iwe
->u
.qual
.noise
;
1207 res
->res
.level
= iwe
->u
.qual
.level
;
1211 static void wext_get_scan_encode(struct iw_event
*iwe
,
1212 struct wext_scan_data
*res
)
1214 if (!(iwe
->u
.data
.flags
& IW_ENCODE_DISABLED
))
1215 res
->res
.caps
|= IEEE80211_CAP_PRIVACY
;
1219 static void wext_get_scan_rate(struct iw_event
*iwe
,
1220 struct wext_scan_data
*res
, char *pos
,
1224 char *custom
= pos
+ IW_EV_LCP_LEN
;
1229 if (custom
+ clen
> end
)
1232 while (((ssize_t
) clen
) >= (ssize_t
) sizeof(struct iw_param
)) {
1233 /* Note: may be misaligned, make a local, aligned copy */
1234 os_memcpy(&p
, custom
, sizeof(struct iw_param
));
1235 if (p
.value
> maxrate
)
1237 clen
-= sizeof(struct iw_param
);
1238 custom
+= sizeof(struct iw_param
);
1241 /* Convert the maxrate from WE-style (b/s units) to
1242 * 802.11 rates (500000 b/s units).
1244 res
->maxrate
= maxrate
/ 500000;
1248 static void wext_get_scan_iwevgenie(struct iw_event
*iwe
,
1249 struct wext_scan_data
*res
, char *custom
,
1252 char *genie
, *gpos
, *gend
;
1255 gpos
= genie
= custom
;
1256 gend
= genie
+ iwe
->u
.data
.length
;
1258 wpa_printf(MSG_INFO
, "IWEVGENIE overflow");
1262 tmp
= os_realloc(res
->ie
, res
->ie_len
+ gend
- gpos
);
1265 os_memcpy(tmp
+ res
->ie_len
, gpos
, gend
- gpos
);
1267 res
->ie_len
+= gend
- gpos
;
1271 static void wext_get_scan_custom(struct iw_event
*iwe
,
1272 struct wext_scan_data
*res
, char *custom
,
1278 clen
= iwe
->u
.data
.length
;
1279 if (custom
+ clen
> end
)
1282 if (clen
> 7 && os_strncmp(custom
, "wpa_ie=", 7) == 0) {
1286 bytes
= custom
+ clen
- spos
;
1290 tmp
= os_realloc(res
->ie
, res
->ie_len
+ bytes
);
1293 hexstr2bin(spos
, tmp
+ res
->ie_len
, bytes
);
1295 res
->ie_len
+= bytes
;
1296 } else if (clen
> 7 && os_strncmp(custom
, "rsn_ie=", 7) == 0) {
1300 bytes
= custom
+ clen
- spos
;
1304 tmp
= os_realloc(res
->ie
, res
->ie_len
+ bytes
);
1307 hexstr2bin(spos
, tmp
+ res
->ie_len
, bytes
);
1309 res
->ie_len
+= bytes
;
1310 } else if (clen
> 4 && os_strncmp(custom
, "tsf=", 4) == 0) {
1315 bytes
= custom
+ clen
- spos
;
1317 wpa_printf(MSG_INFO
, "Invalid TSF length (%d)", bytes
);
1321 hexstr2bin(spos
, bin
, bytes
);
1322 res
->res
.tsf
+= WPA_GET_BE64(bin
);
1327 static int wext_19_iw_point(struct wpa_driver_nl80211_data
*drv
, u16 cmd
)
1329 return drv
->we_version_compiled
> 18 &&
1330 (cmd
== SIOCGIWESSID
|| cmd
== SIOCGIWENCODE
||
1331 cmd
== IWEVGENIE
|| cmd
== IWEVCUSTOM
);
1335 static void wpa_driver_nl80211_add_scan_entry(struct wpa_scan_results
*res
,
1336 struct wext_scan_data
*data
)
1338 struct wpa_scan_res
**tmp
;
1339 struct wpa_scan_res
*r
;
1341 u8
*pos
, *end
, *ssid_ie
= NULL
, *rate_ie
= NULL
;
1343 /* Figure out whether we need to fake any IEs */
1345 end
= pos
+ data
->ie_len
;
1346 while (pos
&& pos
+ 1 < end
) {
1347 if (pos
+ 2 + pos
[1] > end
)
1349 if (pos
[0] == WLAN_EID_SSID
)
1351 else if (pos
[0] == WLAN_EID_SUPP_RATES
)
1353 else if (pos
[0] == WLAN_EID_EXT_SUPP_RATES
)
1359 if (ssid_ie
== NULL
)
1360 extra_len
+= 2 + data
->ssid_len
;
1361 if (rate_ie
== NULL
&& data
->maxrate
)
1364 r
= os_zalloc(sizeof(*r
) + extra_len
+ data
->ie_len
);
1367 os_memcpy(r
, &data
->res
, sizeof(*r
));
1368 r
->ie_len
= extra_len
+ data
->ie_len
;
1369 pos
= (u8
*) (r
+ 1);
1370 if (ssid_ie
== NULL
) {
1372 * Generate a fake SSID IE since the driver did not report
1375 *pos
++ = WLAN_EID_SSID
;
1376 *pos
++ = data
->ssid_len
;
1377 os_memcpy(pos
, data
->ssid
, data
->ssid_len
);
1378 pos
+= data
->ssid_len
;
1380 if (rate_ie
== NULL
&& data
->maxrate
) {
1382 * Generate a fake Supported Rates IE since the driver did not
1383 * report a full IE list.
1385 *pos
++ = WLAN_EID_SUPP_RATES
;
1387 *pos
++ = data
->maxrate
;
1390 os_memcpy(pos
, data
->ie
, data
->ie_len
);
1392 tmp
= os_realloc(res
->res
,
1393 (res
->num
+ 1) * sizeof(struct wpa_scan_res
*));
1398 tmp
[res
->num
++] = r
;
1404 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1405 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1406 * Returns: Scan results on success, -1 on failure
1408 struct wpa_scan_results
* wpa_driver_nl80211_get_scan_results(void *priv
)
1410 struct wpa_driver_nl80211_data
*drv
= priv
;
1411 size_t ap_num
= 0, len
;
1414 struct iw_event iwe_buf
, *iwe
= &iwe_buf
;
1415 char *pos
, *end
, *custom
;
1416 struct wpa_scan_results
*res
;
1417 struct wext_scan_data data
;
1419 res_buf
= wpa_driver_nl80211_giwscan(drv
, &len
);
1420 if (res_buf
== NULL
)
1426 res
= os_zalloc(sizeof(*res
));
1432 pos
= (char *) res_buf
;
1433 end
= (char *) res_buf
+ len
;
1434 os_memset(&data
, 0, sizeof(data
));
1436 while (pos
+ IW_EV_LCP_LEN
<= end
) {
1437 /* Event data may be unaligned, so make a local, aligned copy
1438 * before processing. */
1439 os_memcpy(&iwe_buf
, pos
, IW_EV_LCP_LEN
);
1440 if (iwe
->len
<= IW_EV_LCP_LEN
)
1443 custom
= pos
+ IW_EV_POINT_LEN
;
1444 if (wext_19_iw_point(drv
, iwe
->cmd
)) {
1445 /* WE-19 removed the pointer from struct iw_point */
1446 char *dpos
= (char *) &iwe_buf
.u
.data
.length
;
1447 int dlen
= dpos
- (char *) &iwe_buf
;
1448 os_memcpy(dpos
, pos
+ IW_EV_LCP_LEN
,
1449 sizeof(struct iw_event
) - dlen
);
1451 os_memcpy(&iwe_buf
, pos
, sizeof(struct iw_event
));
1452 custom
+= IW_EV_POINT_OFF
;
1458 wpa_driver_nl80211_add_scan_entry(res
, &data
);
1461 os_memset(&data
, 0, sizeof(data
));
1462 os_memcpy(data
.res
.bssid
,
1463 iwe
->u
.ap_addr
.sa_data
, ETH_ALEN
);
1466 wext_get_scan_mode(iwe
, &data
);
1469 wext_get_scan_ssid(iwe
, &data
, custom
, end
);
1472 wext_get_scan_freq(iwe
, &data
);
1475 wext_get_scan_qual(iwe
, &data
);
1478 wext_get_scan_encode(iwe
, &data
);
1481 wext_get_scan_rate(iwe
, &data
, pos
, end
);
1484 wext_get_scan_iwevgenie(iwe
, &data
, custom
, end
);
1487 wext_get_scan_custom(iwe
, &data
, custom
, end
);
1496 wpa_driver_nl80211_add_scan_entry(res
, &data
);
1499 wpa_printf(MSG_DEBUG
, "Received %lu bytes of scan results (%lu BSSes)",
1500 (unsigned long) len
, (unsigned long) res
->num
);
1506 static int wpa_driver_nl80211_get_range(void *priv
)
1508 struct wpa_driver_nl80211_data
*drv
= priv
;
1509 struct iw_range
*range
;
1515 * Use larger buffer than struct iw_range in order to allow the
1516 * structure to grow in the future.
1518 buflen
= sizeof(struct iw_range
) + 500;
1519 range
= os_zalloc(buflen
);
1523 os_memset(&iwr
, 0, sizeof(iwr
));
1524 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1525 iwr
.u
.data
.pointer
= (caddr_t
) range
;
1526 iwr
.u
.data
.length
= buflen
;
1528 minlen
= ((char *) &range
->enc_capa
) - (char *) range
+
1529 sizeof(range
->enc_capa
);
1531 if (ioctl(drv
->ioctl_sock
, SIOCGIWRANGE
, &iwr
) < 0) {
1532 perror("ioctl[SIOCGIWRANGE]");
1535 } else if (iwr
.u
.data
.length
>= minlen
&&
1536 range
->we_version_compiled
>= 18) {
1537 wpa_printf(MSG_DEBUG
, "SIOCGIWRANGE: WE(compiled)=%d "
1538 "WE(source)=%d enc_capa=0x%x",
1539 range
->we_version_compiled
,
1540 range
->we_version_source
,
1542 drv
->has_capability
= 1;
1543 drv
->we_version_compiled
= range
->we_version_compiled
;
1544 if (range
->enc_capa
& IW_ENC_CAPA_WPA
) {
1545 drv
->capa
.key_mgmt
|= WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
1546 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
;
1548 if (range
->enc_capa
& IW_ENC_CAPA_WPA2
) {
1549 drv
->capa
.key_mgmt
|= WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
1550 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
;
1552 drv
->capa
.enc
|= WPA_DRIVER_CAPA_ENC_WEP40
|
1553 WPA_DRIVER_CAPA_ENC_WEP104
;
1554 if (range
->enc_capa
& IW_ENC_CAPA_CIPHER_TKIP
)
1555 drv
->capa
.enc
|= WPA_DRIVER_CAPA_ENC_TKIP
;
1556 if (range
->enc_capa
& IW_ENC_CAPA_CIPHER_CCMP
)
1557 drv
->capa
.enc
|= WPA_DRIVER_CAPA_ENC_CCMP
;
1558 wpa_printf(MSG_DEBUG
, " capabilities: key_mgmt 0x%x enc 0x%x",
1559 drv
->capa
.key_mgmt
, drv
->capa
.enc
);
1561 wpa_printf(MSG_DEBUG
, "SIOCGIWRANGE: too old (short) data - "
1562 "assuming WPA is not supported");
1570 static int wpa_driver_nl80211_set_wpa(void *priv
, int enabled
)
1572 struct wpa_driver_nl80211_data
*drv
= priv
;
1573 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
1575 return wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_WPA_ENABLED
,
1580 static int wpa_driver_nl80211_set_key(void *priv
, wpa_alg alg
,
1581 const u8
*addr
, int key_idx
,
1582 int set_tx
, const u8
*seq
,
1584 const u8
*key
, size_t key_len
)
1586 struct wpa_driver_nl80211_data
*drv
= priv
;
1590 wpa_printf(MSG_DEBUG
, "%s: alg=%d addr=%p key_idx=%d set_tx=%d "
1591 "seq_len=%lu key_len=%lu",
1592 __func__
, alg
, addr
, key_idx
, set_tx
,
1593 (unsigned long) seq_len
, (unsigned long) key_len
);
1595 msg
= nlmsg_alloc();
1599 if (alg
== WPA_ALG_NONE
) {
1600 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1601 NL80211_CMD_DEL_KEY
, 0);
1603 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1604 NL80211_CMD_NEW_KEY
, 0);
1605 NLA_PUT(msg
, NL80211_ATTR_KEY_DATA
, key_len
, key
);
1609 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1612 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1616 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC02);
1619 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC04);
1627 if (addr
&& os_memcmp(addr
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
1629 wpa_printf(MSG_DEBUG
, " addr=" MACSTR
, MAC2STR(addr
));
1630 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1632 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1633 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1636 if (nl_send_auto_complete(drv
->nl_handle
, msg
) < 0 ||
1637 (err
= nl_wait_for_ack(drv
->nl_handle
)) < 0) {
1638 wpa_printf(MSG_DEBUG
, "nl80211: set_key failed; err=%d", err
);
1643 if (set_tx
&& alg
!= WPA_ALG_NONE
) {
1645 msg
= nlmsg_alloc();
1649 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1650 0, NL80211_CMD_SET_KEY
, 0);
1651 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1652 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1653 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT
);
1656 if (nl_send_auto_complete(drv
->nl_handle
, msg
) < 0 ||
1657 (err
= nl_wait_for_ack(drv
->nl_handle
)) < 0) {
1658 wpa_printf(MSG_DEBUG
, "nl80211: set default key "
1659 "failed; err=%d", err
);
1673 static int wpa_driver_nl80211_set_countermeasures(void *priv
,
1676 struct wpa_driver_nl80211_data
*drv
= priv
;
1677 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
1678 return wpa_driver_nl80211_set_auth_param(drv
,
1679 IW_AUTH_TKIP_COUNTERMEASURES
,
1684 static int wpa_driver_nl80211_set_drop_unencrypted(void *priv
,
1687 struct wpa_driver_nl80211_data
*drv
= priv
;
1688 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
1689 drv
->use_crypt
= enabled
;
1690 return wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_DROP_UNENCRYPTED
,
1695 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data
*drv
,
1696 const u8
*addr
, int cmd
, int reason_code
)
1699 struct iw_mlme mlme
;
1702 os_memset(&iwr
, 0, sizeof(iwr
));
1703 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1704 os_memset(&mlme
, 0, sizeof(mlme
));
1706 mlme
.reason_code
= reason_code
;
1707 mlme
.addr
.sa_family
= ARPHRD_ETHER
;
1708 os_memcpy(mlme
.addr
.sa_data
, addr
, ETH_ALEN
);
1709 iwr
.u
.data
.pointer
= (caddr_t
) &mlme
;
1710 iwr
.u
.data
.length
= sizeof(mlme
);
1712 if (ioctl(drv
->ioctl_sock
, SIOCSIWMLME
, &iwr
) < 0) {
1713 perror("ioctl[SIOCSIWMLME]");
1721 static int wpa_driver_nl80211_deauthenticate(void *priv
, const u8
*addr
,
1724 struct wpa_driver_nl80211_data
*drv
= priv
;
1725 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
1726 return wpa_driver_nl80211_mlme(drv
, addr
, IW_MLME_DEAUTH
, reason_code
);
1730 static int wpa_driver_nl80211_disassociate(void *priv
, const u8
*addr
,
1733 struct wpa_driver_nl80211_data
*drv
= priv
;
1734 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
1735 return wpa_driver_nl80211_mlme(drv
, addr
, IW_MLME_DISASSOC
,
1740 static int wpa_driver_nl80211_set_gen_ie(void *priv
, const u8
*ie
,
1743 struct wpa_driver_nl80211_data
*drv
= priv
;
1747 os_memset(&iwr
, 0, sizeof(iwr
));
1748 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1749 iwr
.u
.data
.pointer
= (caddr_t
) ie
;
1750 iwr
.u
.data
.length
= ie_len
;
1752 if (ioctl(drv
->ioctl_sock
, SIOCSIWGENIE
, &iwr
) < 0) {
1753 perror("ioctl[SIOCSIWGENIE]");
1761 static int wpa_driver_nl80211_cipher2wext(int cipher
)
1765 return IW_AUTH_CIPHER_NONE
;
1767 return IW_AUTH_CIPHER_WEP40
;
1769 return IW_AUTH_CIPHER_TKIP
;
1771 return IW_AUTH_CIPHER_CCMP
;
1773 return IW_AUTH_CIPHER_WEP104
;
1780 static int wpa_driver_nl80211_keymgmt2wext(int keymgmt
)
1783 case KEY_MGMT_802_1X
:
1784 case KEY_MGMT_802_1X_NO_WPA
:
1785 return IW_AUTH_KEY_MGMT_802_1X
;
1787 return IW_AUTH_KEY_MGMT_PSK
;
1795 wpa_driver_nl80211_auth_alg_fallback(struct wpa_driver_nl80211_data
*drv
,
1796 struct wpa_driver_associate_params
*params
)
1801 wpa_printf(MSG_DEBUG
, "WEXT: Driver did not support "
1802 "SIOCSIWAUTH for AUTH_ALG, trying SIOCSIWENCODE");
1804 os_memset(&iwr
, 0, sizeof(iwr
));
1805 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1806 /* Just changing mode, not actual keys */
1807 iwr
.u
.encoding
.flags
= 0;
1808 iwr
.u
.encoding
.pointer
= (caddr_t
) NULL
;
1809 iwr
.u
.encoding
.length
= 0;
1812 * Note: IW_ENCODE_{OPEN,RESTRICTED} can be interpreted to mean two
1813 * different things. Here they are used to indicate Open System vs.
1814 * Shared Key authentication algorithm. However, some drivers may use
1815 * them to select between open/restricted WEP encrypted (open = allow
1816 * both unencrypted and encrypted frames; restricted = only allow
1817 * encrypted frames).
1820 if (!drv
->use_crypt
) {
1821 iwr
.u
.encoding
.flags
|= IW_ENCODE_DISABLED
;
1823 if (params
->auth_alg
& AUTH_ALG_OPEN_SYSTEM
)
1824 iwr
.u
.encoding
.flags
|= IW_ENCODE_OPEN
;
1825 if (params
->auth_alg
& AUTH_ALG_SHARED_KEY
)
1826 iwr
.u
.encoding
.flags
|= IW_ENCODE_RESTRICTED
;
1829 if (ioctl(drv
->ioctl_sock
, SIOCSIWENCODE
, &iwr
) < 0) {
1830 perror("ioctl[SIOCSIWENCODE]");
1838 static int wpa_driver_nl80211_associate(
1839 void *priv
, struct wpa_driver_associate_params
*params
)
1841 struct wpa_driver_nl80211_data
*drv
= priv
;
1843 int allow_unencrypted_eapol
;
1846 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
1849 * If the driver did not support SIOCSIWAUTH, fallback to
1850 * SIOCSIWENCODE here.
1852 if (drv
->auth_alg_fallback
&&
1853 wpa_driver_nl80211_auth_alg_fallback(drv
, params
) < 0)
1856 if (!params
->bssid
&&
1857 wpa_driver_nl80211_set_bssid(drv
, NULL
) < 0)
1860 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
) < 0)
1862 /* TODO: should consider getting wpa version and cipher/key_mgmt suites
1863 * from configuration, not from here, where only the selected suite is
1865 if (wpa_driver_nl80211_set_gen_ie(drv
, params
->wpa_ie
, params
->wpa_ie_len
)
1868 if (params
->wpa_ie
== NULL
|| params
->wpa_ie_len
== 0)
1869 value
= IW_AUTH_WPA_VERSION_DISABLED
;
1870 else if (params
->wpa_ie
[0] == WLAN_EID_RSN
)
1871 value
= IW_AUTH_WPA_VERSION_WPA2
;
1873 value
= IW_AUTH_WPA_VERSION_WPA
;
1874 if (wpa_driver_nl80211_set_auth_param(drv
,
1875 IW_AUTH_WPA_VERSION
, value
) < 0)
1877 value
= wpa_driver_nl80211_cipher2wext(params
->pairwise_suite
);
1878 if (wpa_driver_nl80211_set_auth_param(drv
,
1879 IW_AUTH_CIPHER_PAIRWISE
, value
) < 0)
1881 value
= wpa_driver_nl80211_cipher2wext(params
->group_suite
);
1882 if (wpa_driver_nl80211_set_auth_param(drv
,
1883 IW_AUTH_CIPHER_GROUP
, value
) < 0)
1885 value
= wpa_driver_nl80211_keymgmt2wext(params
->key_mgmt_suite
);
1886 if (wpa_driver_nl80211_set_auth_param(drv
,
1887 IW_AUTH_KEY_MGMT
, value
) < 0)
1889 value
= params
->key_mgmt_suite
!= KEY_MGMT_NONE
||
1890 params
->pairwise_suite
!= CIPHER_NONE
||
1891 params
->group_suite
!= CIPHER_NONE
||
1893 if (wpa_driver_nl80211_set_auth_param(drv
,
1894 IW_AUTH_PRIVACY_INVOKED
, value
) < 0)
1897 /* Allow unencrypted EAPOL messages even if pairwise keys are set when
1898 * not using WPA. IEEE 802.1X specifies that these frames are not
1899 * encrypted, but WPA encrypts them when pairwise keys are in use. */
1900 if (params
->key_mgmt_suite
== KEY_MGMT_802_1X
||
1901 params
->key_mgmt_suite
== KEY_MGMT_PSK
)
1902 allow_unencrypted_eapol
= 0;
1904 allow_unencrypted_eapol
= 1;
1906 if (wpa_driver_nl80211_set_auth_param(drv
,
1907 IW_AUTH_RX_UNENCRYPTED_EAPOL
,
1908 allow_unencrypted_eapol
) < 0)
1910 if (params
->freq
&& wpa_driver_nl80211_set_freq(drv
, params
->freq
) < 0)
1912 if (wpa_driver_nl80211_set_ssid(drv
, params
->ssid
, params
->ssid_len
) < 0)
1914 if (params
->bssid
&&
1915 wpa_driver_nl80211_set_bssid(drv
, params
->bssid
) < 0)
1922 static int wpa_driver_nl80211_set_auth_alg(void *priv
, int auth_alg
)
1924 struct wpa_driver_nl80211_data
*drv
= priv
;
1927 if (auth_alg
& AUTH_ALG_OPEN_SYSTEM
)
1928 algs
|= IW_AUTH_ALG_OPEN_SYSTEM
;
1929 if (auth_alg
& AUTH_ALG_SHARED_KEY
)
1930 algs
|= IW_AUTH_ALG_SHARED_KEY
;
1931 if (auth_alg
& AUTH_ALG_LEAP
)
1932 algs
|= IW_AUTH_ALG_LEAP
;
1934 /* at least one algorithm should be set */
1935 algs
= IW_AUTH_ALG_OPEN_SYSTEM
;
1938 res
= wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_80211_AUTH_ALG
,
1940 drv
->auth_alg_fallback
= res
== -2;
1946 * wpa_driver_nl80211_set_mode - Set wireless mode (infra/adhoc), SIOCSIWMODE
1947 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1948 * @mode: 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
1949 * Returns: 0 on success, -1 on failure
1951 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
)
1953 struct wpa_driver_nl80211_data
*drv
= priv
;
1954 int ret
= -1, flags
;
1957 msg
= nlmsg_alloc();
1961 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1962 0, NL80211_CMD_SET_INTERFACE
, 0);
1963 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1964 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_AP
);
1966 if (nl_send_auto_complete(drv
->nl_handle
, msg
) < 0 ||
1967 nl_wait_for_ack(drv
->nl_handle
) < 0)
1978 /* mac80211 doesn't allow mode changes while the device is up, so
1979 * take the device down, try to set the mode again, and bring the
1982 if (wpa_driver_nl80211_get_ifflags(drv
, &flags
) == 0) {
1983 (void) wpa_driver_nl80211_set_ifflags(drv
, flags
& ~IFF_UP
);
1985 /* Try to set the mode again while the interface is down */
1986 if (nl_send_auto_complete(drv
->nl_handle
, msg
) < 0 ||
1987 nl_wait_for_ack(drv
->nl_handle
) < 0) {
1988 wpa_printf(MSG_ERROR
, "Failed to set interface %s "
1989 "mode", drv
->ifname
);
1993 /* Ignore return value of get_ifflags to ensure that the device
1994 * is always up like it was before this function was called.
1996 (void) wpa_driver_nl80211_get_ifflags(drv
, &flags
);
1997 (void) wpa_driver_nl80211_set_ifflags(drv
, flags
| IFF_UP
);
2005 static int wpa_driver_nl80211_pmksa(struct wpa_driver_nl80211_data
*drv
,
2006 u32 cmd
, const u8
*bssid
, const u8
*pmkid
)
2009 struct iw_pmksa pmksa
;
2012 os_memset(&iwr
, 0, sizeof(iwr
));
2013 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
2014 os_memset(&pmksa
, 0, sizeof(pmksa
));
2016 pmksa
.bssid
.sa_family
= ARPHRD_ETHER
;
2018 os_memcpy(pmksa
.bssid
.sa_data
, bssid
, ETH_ALEN
);
2020 os_memcpy(pmksa
.pmkid
, pmkid
, IW_PMKID_LEN
);
2021 iwr
.u
.data
.pointer
= (caddr_t
) &pmksa
;
2022 iwr
.u
.data
.length
= sizeof(pmksa
);
2024 if (ioctl(drv
->ioctl_sock
, SIOCSIWPMKSA
, &iwr
) < 0) {
2025 if (errno
!= EOPNOTSUPP
)
2026 perror("ioctl[SIOCSIWPMKSA]");
2034 static int wpa_driver_nl80211_add_pmkid(void *priv
, const u8
*bssid
,
2037 struct wpa_driver_nl80211_data
*drv
= priv
;
2038 return wpa_driver_nl80211_pmksa(drv
, IW_PMKSA_ADD
, bssid
, pmkid
);
2042 static int wpa_driver_nl80211_remove_pmkid(void *priv
, const u8
*bssid
,
2045 struct wpa_driver_nl80211_data
*drv
= priv
;
2046 return wpa_driver_nl80211_pmksa(drv
, IW_PMKSA_REMOVE
, bssid
, pmkid
);
2050 static int wpa_driver_nl80211_flush_pmkid(void *priv
)
2052 struct wpa_driver_nl80211_data
*drv
= priv
;
2053 return wpa_driver_nl80211_pmksa(drv
, IW_PMKSA_FLUSH
, NULL
, NULL
);
2057 static int wpa_driver_nl80211_get_capa(void *priv
,
2058 struct wpa_driver_capa
*capa
)
2060 struct wpa_driver_nl80211_data
*drv
= priv
;
2061 if (!drv
->has_capability
)
2063 os_memcpy(capa
, &drv
->capa
, sizeof(*capa
));
2068 static int wpa_driver_nl80211_set_operstate(void *priv
, int state
)
2070 struct wpa_driver_nl80211_data
*drv
= priv
;
2072 wpa_printf(MSG_DEBUG
, "%s: operstate %d->%d (%s)",
2073 __func__
, drv
->operstate
, state
, state
? "UP" : "DORMANT");
2074 drv
->operstate
= state
;
2075 return wpa_driver_nl80211_send_oper_ifla(
2076 drv
, -1, state
? IF_OPER_UP
: IF_OPER_DORMANT
);
2080 const struct wpa_driver_ops wpa_driver_nl80211_ops
= {
2082 .desc
= "Linux nl80211/cfg80211",
2083 .get_bssid
= wpa_driver_nl80211_get_bssid
,
2084 .get_ssid
= wpa_driver_nl80211_get_ssid
,
2085 .set_wpa
= wpa_driver_nl80211_set_wpa
,
2086 .set_key
= wpa_driver_nl80211_set_key
,
2087 .set_countermeasures
= wpa_driver_nl80211_set_countermeasures
,
2088 .set_drop_unencrypted
= wpa_driver_nl80211_set_drop_unencrypted
,
2089 .scan
= wpa_driver_nl80211_scan
,
2090 .get_scan_results2
= wpa_driver_nl80211_get_scan_results
,
2091 .deauthenticate
= wpa_driver_nl80211_deauthenticate
,
2092 .disassociate
= wpa_driver_nl80211_disassociate
,
2093 .associate
= wpa_driver_nl80211_associate
,
2094 .set_auth_alg
= wpa_driver_nl80211_set_auth_alg
,
2095 .init
= wpa_driver_nl80211_init
,
2096 .deinit
= wpa_driver_nl80211_deinit
,
2097 .add_pmkid
= wpa_driver_nl80211_add_pmkid
,
2098 .remove_pmkid
= wpa_driver_nl80211_remove_pmkid
,
2099 .flush_pmkid
= wpa_driver_nl80211_flush_pmkid
,
2100 .get_capa
= wpa_driver_nl80211_get_capa
,
2101 .set_operstate
= wpa_driver_nl80211_set_operstate
,