2 * WPA Supplicant - driver interaction with generic Linux Wireless Extensions
3 * Copyright (c) 2003-2007, 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.
14 * This file implements a driver interface for the Linux Wireless Extensions.
15 * When used with WE-18 or newer, this interface can be used as-is with number
16 * of drivers. In addition to this, some of the common functions in this file
17 * can be used by other driver interface implementations that use generic WE
18 * ioctls, but require private ioctls for some of the functionality.
22 #include <sys/ioctl.h>
23 #include <net/if_arp.h>
25 #include "wireless_copy.h"
29 #include "priv_netlink.h"
30 #include "driver_wext.h"
31 #include "ieee802_11_defs.h"
32 #include "wpa_common.h"
34 #ifdef CONFIG_CLIENT_MLME
35 #include <netpacket/packet.h>
36 /* old definitions from net/mac80211 */
38 typedef u32 __bitwise __be32
;
39 typedef u64 __bitwise __be64
;
41 #define PRISM2_IOCTL_PRISM2_PARAM (SIOCIWFIRSTPRIV + 0)
42 #define PRISM2_IOCTL_GET_PRISM2_PARAM (SIOCIWFIRSTPRIV + 1)
43 #define PRISM2_IOCTL_HOSTAPD (SIOCIWFIRSTPRIV + 3)
45 #define PRISM2_PARAM_USER_SPACE_MLME 1045
46 #define PRISM2_PARAM_MGMT_IF 1046
47 #define PRISM2_HOSTAPD_ADD_STA 2
48 #define PRISM2_HOSTAPD_REMOVE_STA 3
49 #define PRISM2_HOSTAPD_GET_HW_FEATURES 1002
50 #define PRISM2_HOSTAPD_MAX_BUF_SIZE 2048
53 #define ALIGNED __attribute__ ((aligned))
56 struct prism2_hostapd_param
{
58 u8 sta_addr
[ETH_ALEN
];
66 #define IEEE80211_STA_DYNAMIC_ENC BIT(0)
73 u8 data
[0] ALIGNED
; /* num_modes * feature data */
76 u16 mode
; /* MODE_* */
77 u16 num_supported_rates
;
79 u8 data
[0] ALIGNED
; /* num_supported_rates * u16 +
80 * num_basic_rates * u16 */
83 u16 mode
; /* MODE_* */
86 u8 power_level
; /* regulatory limit in dBm */
91 } set_regulatory_domain
;
97 u32 burst_time
; /* maximum burst time in 0.1 ms, i.e.,
103 struct hostapd_ioctl_hw_modes_hdr
{
110 * frame format for the management interface that is slated
111 * to be replaced by "cooked monitor" with radiotap
113 #define IEEE80211_FI_VERSION 0x80211001
114 struct ieee80211_frame_info
{
130 /* Note: this structure is otherwise identical to capture format used
131 * in linux-wlan-ng, but this additional field is used to provide meta
132 * data about the frame to hostapd. This was the easiest method for
133 * providing this information, but this might change in the future. */
135 } __attribute__ ((packed
));
137 /* old mode definitions */
139 MODE_IEEE80211A
= 0 /* IEEE 802.11a */,
140 MODE_IEEE80211B
= 1 /* IEEE 802.11b only */,
141 MODE_ATHEROS_TURBO
= 2 /* Atheros Turbo mode (2x.11a at 5 GHz) */,
142 MODE_IEEE80211G
= 3 /* IEEE 802.11g (and 802.11b compatibility) */,
143 MODE_ATHEROS_TURBOG
= 4 /* Atheros Turbo mode (2x.11g at 2.4 GHz) */,
144 NUM_IEEE80211_MODES
= 5
148 #define ETH_P_ALL 0x0003
150 #endif /* CONFIG_CLIENT_MLME */
155 static int wpa_driver_wext_flush_pmkid(void *priv
);
156 static int wpa_driver_wext_get_range(void *priv
);
157 static void wpa_driver_wext_finish_drv_init(struct wpa_driver_wext_data
*drv
);
160 static int wpa_driver_wext_send_oper_ifla(struct wpa_driver_wext_data
*drv
,
161 int linkmode
, int operstate
)
165 struct ifinfomsg ifinfo
;
172 os_memset(&req
, 0, sizeof(req
));
174 req
.hdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
175 req
.hdr
.nlmsg_type
= RTM_SETLINK
;
176 req
.hdr
.nlmsg_flags
= NLM_F_REQUEST
;
177 req
.hdr
.nlmsg_seq
= ++nl_seq
;
178 req
.hdr
.nlmsg_pid
= 0;
180 req
.ifinfo
.ifi_family
= AF_UNSPEC
;
181 req
.ifinfo
.ifi_type
= 0;
182 req
.ifinfo
.ifi_index
= drv
->ifindex
;
183 req
.ifinfo
.ifi_flags
= 0;
184 req
.ifinfo
.ifi_change
= 0;
186 if (linkmode
!= -1) {
187 rta
= (struct rtattr
*)
188 ((char *) &req
+ NLMSG_ALIGN(req
.hdr
.nlmsg_len
));
189 rta
->rta_type
= IFLA_LINKMODE
;
190 rta
->rta_len
= RTA_LENGTH(sizeof(char));
191 *((char *) RTA_DATA(rta
)) = linkmode
;
192 req
.hdr
.nlmsg_len
= NLMSG_ALIGN(req
.hdr
.nlmsg_len
) +
193 RTA_LENGTH(sizeof(char));
195 if (operstate
!= -1) {
196 rta
= (struct rtattr
*)
197 ((char *) &req
+ NLMSG_ALIGN(req
.hdr
.nlmsg_len
));
198 rta
->rta_type
= IFLA_OPERSTATE
;
199 rta
->rta_len
= RTA_LENGTH(sizeof(char));
200 *((char *) RTA_DATA(rta
)) = operstate
;
201 req
.hdr
.nlmsg_len
= NLMSG_ALIGN(req
.hdr
.nlmsg_len
) +
202 RTA_LENGTH(sizeof(char));
205 wpa_printf(MSG_DEBUG
, "WEXT: Operstate: linkmode=%d, operstate=%d",
206 linkmode
, operstate
);
208 ret
= send(drv
->event_sock
, &req
, req
.hdr
.nlmsg_len
, 0);
210 wpa_printf(MSG_DEBUG
, "WEXT: Sending operstate IFLA failed: "
211 "%s (assume operstate is not supported)",
215 return ret
< 0 ? -1 : 0;
219 int wpa_driver_wext_set_auth_param(struct wpa_driver_wext_data
*drv
,
225 os_memset(&iwr
, 0, sizeof(iwr
));
226 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
227 iwr
.u
.param
.flags
= idx
& IW_AUTH_INDEX
;
228 iwr
.u
.param
.value
= value
;
230 if (ioctl(drv
->ioctl_sock
, SIOCSIWAUTH
, &iwr
) < 0) {
231 if (errno
!= EOPNOTSUPP
) {
232 wpa_printf(MSG_DEBUG
, "WEXT: SIOCSIWAUTH(param %d "
233 "value 0x%x) failed: %s)",
234 idx
, value
, strerror(errno
));
236 ret
= errno
== EOPNOTSUPP
? -2 : -1;
244 * wpa_driver_wext_get_bssid - Get BSSID, SIOCGIWAP
245 * @priv: Pointer to private wext data from wpa_driver_wext_init()
246 * @bssid: Buffer for BSSID
247 * Returns: 0 on success, -1 on failure
249 int wpa_driver_wext_get_bssid(void *priv
, u8
*bssid
)
251 struct wpa_driver_wext_data
*drv
= priv
;
255 os_memset(&iwr
, 0, sizeof(iwr
));
256 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
258 if (ioctl(drv
->ioctl_sock
, SIOCGIWAP
, &iwr
) < 0) {
259 perror("ioctl[SIOCGIWAP]");
262 os_memcpy(bssid
, iwr
.u
.ap_addr
.sa_data
, ETH_ALEN
);
269 * wpa_driver_wext_set_bssid - Set BSSID, SIOCSIWAP
270 * @priv: Pointer to private wext data from wpa_driver_wext_init()
272 * Returns: 0 on success, -1 on failure
274 int wpa_driver_wext_set_bssid(void *priv
, const u8
*bssid
)
276 struct wpa_driver_wext_data
*drv
= priv
;
280 os_memset(&iwr
, 0, sizeof(iwr
));
281 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
282 iwr
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
284 os_memcpy(iwr
.u
.ap_addr
.sa_data
, bssid
, ETH_ALEN
);
286 os_memset(iwr
.u
.ap_addr
.sa_data
, 0, ETH_ALEN
);
288 if (ioctl(drv
->ioctl_sock
, SIOCSIWAP
, &iwr
) < 0) {
289 perror("ioctl[SIOCSIWAP]");
298 * wpa_driver_wext_get_ssid - Get SSID, SIOCGIWESSID
299 * @priv: Pointer to private wext data from wpa_driver_wext_init()
300 * @ssid: Buffer for the SSID; must be at least 32 bytes long
301 * Returns: SSID length on success, -1 on failure
303 int wpa_driver_wext_get_ssid(void *priv
, u8
*ssid
)
305 struct wpa_driver_wext_data
*drv
= priv
;
309 os_memset(&iwr
, 0, sizeof(iwr
));
310 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
311 iwr
.u
.essid
.pointer
= (caddr_t
) ssid
;
312 iwr
.u
.essid
.length
= 32;
314 if (ioctl(drv
->ioctl_sock
, SIOCGIWESSID
, &iwr
) < 0) {
315 perror("ioctl[SIOCGIWESSID]");
318 ret
= iwr
.u
.essid
.length
;
321 /* Some drivers include nul termination in the SSID, so let's
322 * remove it here before further processing. WE-21 changes this
323 * to explicitly require the length _not_ to include nul
325 if (ret
> 0 && ssid
[ret
- 1] == '\0' &&
326 drv
->we_version_compiled
< 21)
335 * wpa_driver_wext_set_ssid - Set SSID, SIOCSIWESSID
336 * @priv: Pointer to private wext data from wpa_driver_wext_init()
338 * @ssid_len: Length of SSID (0..32)
339 * Returns: 0 on success, -1 on failure
341 int wpa_driver_wext_set_ssid(void *priv
, const u8
*ssid
, size_t ssid_len
)
343 struct wpa_driver_wext_data
*drv
= priv
;
351 os_memset(&iwr
, 0, sizeof(iwr
));
352 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
353 /* flags: 1 = ESSID is active, 0 = not (promiscuous) */
354 iwr
.u
.essid
.flags
= (ssid_len
!= 0);
355 os_memset(buf
, 0, sizeof(buf
));
356 os_memcpy(buf
, ssid
, ssid_len
);
357 iwr
.u
.essid
.pointer
= (caddr_t
) buf
;
358 if (drv
->we_version_compiled
< 21) {
359 /* For historic reasons, set SSID length to include one extra
360 * character, C string nul termination, even though SSID is
361 * really an octet string that should not be presented as a C
362 * string. Some Linux drivers decrement the length by one and
363 * can thus end up missing the last octet of the SSID if the
364 * length is not incremented here. WE-21 changes this to
365 * explicitly require the length _not_ to include nul
370 iwr
.u
.essid
.length
= ssid_len
;
372 if (ioctl(drv
->ioctl_sock
, SIOCSIWESSID
, &iwr
) < 0) {
373 perror("ioctl[SIOCSIWESSID]");
382 * wpa_driver_wext_set_freq - Set frequency/channel, SIOCSIWFREQ
383 * @priv: Pointer to private wext data from wpa_driver_wext_init()
384 * @freq: Frequency in MHz
385 * Returns: 0 on success, -1 on failure
387 int wpa_driver_wext_set_freq(void *priv
, int freq
)
389 struct wpa_driver_wext_data
*drv
= priv
;
393 os_memset(&iwr
, 0, sizeof(iwr
));
394 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
395 iwr
.u
.freq
.m
= freq
* 100000;
398 if (ioctl(drv
->ioctl_sock
, SIOCSIWFREQ
, &iwr
) < 0) {
399 perror("ioctl[SIOCSIWFREQ]");
408 wpa_driver_wext_event_wireless_custom(void *ctx
, char *custom
)
410 union wpa_event_data data
;
412 wpa_printf(MSG_MSGDUMP
, "WEXT: Custom wireless event: '%s'",
415 os_memset(&data
, 0, sizeof(data
));
417 if (os_strncmp(custom
, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
418 data
.michael_mic_failure
.unicast
=
419 os_strstr(custom
, " unicast ") != NULL
;
420 /* TODO: parse parameters(?) */
421 wpa_supplicant_event(ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
422 } else if (os_strncmp(custom
, "ASSOCINFO(ReqIEs=", 17) == 0) {
428 bytes
= strspn(spos
, "0123456789abcdefABCDEF");
429 if (!bytes
|| (bytes
& 1))
433 data
.assoc_info
.req_ies
= os_malloc(bytes
);
434 if (data
.assoc_info
.req_ies
== NULL
)
437 data
.assoc_info
.req_ies_len
= bytes
;
438 hexstr2bin(spos
, data
.assoc_info
.req_ies
, bytes
);
442 data
.assoc_info
.resp_ies
= NULL
;
443 data
.assoc_info
.resp_ies_len
= 0;
445 if (os_strncmp(spos
, " RespIEs=", 9) == 0) {
448 bytes
= strspn(spos
, "0123456789abcdefABCDEF");
449 if (!bytes
|| (bytes
& 1))
453 data
.assoc_info
.resp_ies
= os_malloc(bytes
);
454 if (data
.assoc_info
.resp_ies
== NULL
)
457 data
.assoc_info
.resp_ies_len
= bytes
;
458 hexstr2bin(spos
, data
.assoc_info
.resp_ies
, bytes
);
461 wpa_supplicant_event(ctx
, EVENT_ASSOCINFO
, &data
);
464 os_free(data
.assoc_info
.resp_ies
);
465 os_free(data
.assoc_info
.req_ies
);
466 #ifdef CONFIG_PEERKEY
467 } else if (os_strncmp(custom
, "STKSTART.request=", 17) == 0) {
468 if (hwaddr_aton(custom
+ 17, data
.stkstart
.peer
)) {
469 wpa_printf(MSG_DEBUG
, "WEXT: unrecognized "
470 "STKSTART.request '%s'", custom
+ 17);
473 wpa_supplicant_event(ctx
, EVENT_STKSTART
, &data
);
474 #endif /* CONFIG_PEERKEY */
479 static int wpa_driver_wext_event_wireless_michaelmicfailure(
480 void *ctx
, const char *ev
, size_t len
)
482 const struct iw_michaelmicfailure
*mic
;
483 union wpa_event_data data
;
485 if (len
< sizeof(*mic
))
488 mic
= (const struct iw_michaelmicfailure
*) ev
;
490 wpa_printf(MSG_DEBUG
, "Michael MIC failure wireless event: "
491 "flags=0x%x src_addr=" MACSTR
, mic
->flags
,
492 MAC2STR(mic
->src_addr
.sa_data
));
494 os_memset(&data
, 0, sizeof(data
));
495 data
.michael_mic_failure
.unicast
= !(mic
->flags
& IW_MICFAILURE_GROUP
);
496 wpa_supplicant_event(ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
502 static int wpa_driver_wext_event_wireless_pmkidcand(
503 struct wpa_driver_wext_data
*drv
, const char *ev
, size_t len
)
505 const struct iw_pmkid_cand
*cand
;
506 union wpa_event_data data
;
509 if (len
< sizeof(*cand
))
512 cand
= (const struct iw_pmkid_cand
*) ev
;
513 addr
= (const u8
*) cand
->bssid
.sa_data
;
515 wpa_printf(MSG_DEBUG
, "PMKID candidate wireless event: "
516 "flags=0x%x index=%d bssid=" MACSTR
, cand
->flags
,
517 cand
->index
, MAC2STR(addr
));
519 os_memset(&data
, 0, sizeof(data
));
520 os_memcpy(data
.pmkid_candidate
.bssid
, addr
, ETH_ALEN
);
521 data
.pmkid_candidate
.index
= cand
->index
;
522 data
.pmkid_candidate
.preauth
= cand
->flags
& IW_PMKID_CAND_PREAUTH
;
523 wpa_supplicant_event(drv
->ctx
, EVENT_PMKID_CANDIDATE
, &data
);
529 static int wpa_driver_wext_event_wireless_assocreqie(
530 struct wpa_driver_wext_data
*drv
, const char *ev
, int len
)
535 wpa_hexdump(MSG_DEBUG
, "AssocReq IE wireless event", (const u8
*) ev
,
537 os_free(drv
->assoc_req_ies
);
538 drv
->assoc_req_ies
= os_malloc(len
);
539 if (drv
->assoc_req_ies
== NULL
) {
540 drv
->assoc_req_ies_len
= 0;
543 os_memcpy(drv
->assoc_req_ies
, ev
, len
);
544 drv
->assoc_req_ies_len
= len
;
550 static int wpa_driver_wext_event_wireless_assocrespie(
551 struct wpa_driver_wext_data
*drv
, const char *ev
, int len
)
556 wpa_hexdump(MSG_DEBUG
, "AssocResp IE wireless event", (const u8
*) ev
,
558 os_free(drv
->assoc_resp_ies
);
559 drv
->assoc_resp_ies
= os_malloc(len
);
560 if (drv
->assoc_resp_ies
== NULL
) {
561 drv
->assoc_resp_ies_len
= 0;
564 os_memcpy(drv
->assoc_resp_ies
, ev
, len
);
565 drv
->assoc_resp_ies_len
= len
;
571 static void wpa_driver_wext_event_assoc_ies(struct wpa_driver_wext_data
*drv
)
573 union wpa_event_data data
;
575 if (drv
->assoc_req_ies
== NULL
&& drv
->assoc_resp_ies
== NULL
)
578 os_memset(&data
, 0, sizeof(data
));
579 if (drv
->assoc_req_ies
) {
580 data
.assoc_info
.req_ies
= drv
->assoc_req_ies
;
581 drv
->assoc_req_ies
= NULL
;
582 data
.assoc_info
.req_ies_len
= drv
->assoc_req_ies_len
;
584 if (drv
->assoc_resp_ies
) {
585 data
.assoc_info
.resp_ies
= drv
->assoc_resp_ies
;
586 drv
->assoc_resp_ies
= NULL
;
587 data
.assoc_info
.resp_ies_len
= drv
->assoc_resp_ies_len
;
590 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOCINFO
, &data
);
592 os_free(data
.assoc_info
.req_ies
);
593 os_free(data
.assoc_info
.resp_ies
);
597 static void wpa_driver_wext_event_wireless(struct wpa_driver_wext_data
*drv
,
598 void *ctx
, char *data
, int len
)
600 struct iw_event iwe_buf
, *iwe
= &iwe_buf
;
601 char *pos
, *end
, *custom
, *buf
;
606 while (pos
+ IW_EV_LCP_LEN
<= end
) {
607 /* Event data may be unaligned, so make a local, aligned copy
608 * before processing. */
609 os_memcpy(&iwe_buf
, pos
, IW_EV_LCP_LEN
);
610 wpa_printf(MSG_DEBUG
, "Wireless event: cmd=0x%x len=%d",
612 if (iwe
->len
<= IW_EV_LCP_LEN
)
615 custom
= pos
+ IW_EV_POINT_LEN
;
616 if (drv
->we_version_compiled
> 18 &&
617 (iwe
->cmd
== IWEVMICHAELMICFAILURE
||
618 iwe
->cmd
== IWEVCUSTOM
||
619 iwe
->cmd
== IWEVASSOCREQIE
||
620 iwe
->cmd
== IWEVASSOCRESPIE
||
621 iwe
->cmd
== IWEVPMKIDCAND
)) {
622 /* WE-19 removed the pointer from struct iw_point */
623 char *dpos
= (char *) &iwe_buf
.u
.data
.length
;
624 int dlen
= dpos
- (char *) &iwe_buf
;
625 os_memcpy(dpos
, pos
+ IW_EV_LCP_LEN
,
626 sizeof(struct iw_event
) - dlen
);
628 os_memcpy(&iwe_buf
, pos
, sizeof(struct iw_event
));
629 custom
+= IW_EV_POINT_OFF
;
634 wpa_printf(MSG_DEBUG
, "Wireless event: new AP: "
636 MAC2STR((u8
*) iwe
->u
.ap_addr
.sa_data
));
637 if (is_zero_ether_addr(
638 (const u8
*) iwe
->u
.ap_addr
.sa_data
) ||
639 os_memcmp(iwe
->u
.ap_addr
.sa_data
,
640 "\x44\x44\x44\x44\x44\x44", ETH_ALEN
) ==
642 os_free(drv
->assoc_req_ies
);
643 drv
->assoc_req_ies
= NULL
;
644 os_free(drv
->assoc_resp_ies
);
645 drv
->assoc_resp_ies
= NULL
;
646 wpa_supplicant_event(ctx
, EVENT_DISASSOC
,
650 wpa_driver_wext_event_assoc_ies(drv
);
651 wpa_supplicant_event(ctx
, EVENT_ASSOC
, NULL
);
654 case IWEVMICHAELMICFAILURE
:
655 wpa_driver_wext_event_wireless_michaelmicfailure(
656 ctx
, custom
, iwe
->u
.data
.length
);
659 if (custom
+ iwe
->u
.data
.length
> end
)
661 buf
= os_malloc(iwe
->u
.data
.length
+ 1);
664 os_memcpy(buf
, custom
, iwe
->u
.data
.length
);
665 buf
[iwe
->u
.data
.length
] = '\0';
666 wpa_driver_wext_event_wireless_custom(ctx
, buf
);
670 drv
->scan_complete_events
= 1;
671 eloop_cancel_timeout(wpa_driver_wext_scan_timeout
,
673 wpa_supplicant_event(ctx
, EVENT_SCAN_RESULTS
, NULL
);
676 wpa_driver_wext_event_wireless_assocreqie(
677 drv
, custom
, iwe
->u
.data
.length
);
679 case IWEVASSOCRESPIE
:
680 wpa_driver_wext_event_wireless_assocrespie(
681 drv
, custom
, iwe
->u
.data
.length
);
684 wpa_driver_wext_event_wireless_pmkidcand(
685 drv
, custom
, iwe
->u
.data
.length
);
694 static void wpa_driver_wext_event_link(struct wpa_driver_wext_data
*drv
,
695 void *ctx
, char *buf
, size_t len
,
698 union wpa_event_data event
;
700 os_memset(&event
, 0, sizeof(event
));
701 if (len
> sizeof(event
.interface_status
.ifname
))
702 len
= sizeof(event
.interface_status
.ifname
) - 1;
703 os_memcpy(event
.interface_status
.ifname
, buf
, len
);
704 event
.interface_status
.ievent
= del
? EVENT_INTERFACE_REMOVED
:
705 EVENT_INTERFACE_ADDED
;
707 wpa_printf(MSG_DEBUG
, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
709 event
.interface_status
.ifname
,
710 del
? "removed" : "added");
712 if (os_strcmp(drv
->ifname
, event
.interface_status
.ifname
) == 0) {
719 wpa_supplicant_event(ctx
, EVENT_INTERFACE_STATUS
, &event
);
723 static int wpa_driver_wext_own_ifname(struct wpa_driver_wext_data
*drv
,
726 struct ifinfomsg
*ifi
;
727 int attrlen
, nlmsg_len
, rta_len
;
732 nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
734 attrlen
= h
->nlmsg_len
- nlmsg_len
;
738 attr
= (struct rtattr
*) (((char *) ifi
) + nlmsg_len
);
740 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
741 while (RTA_OK(attr
, attrlen
)) {
742 if (attr
->rta_type
== IFLA_IFNAME
) {
743 if (os_strcmp(((char *) attr
) + rta_len
, drv
->ifname
)
749 attr
= RTA_NEXT(attr
, attrlen
);
756 static int wpa_driver_wext_own_ifindex(struct wpa_driver_wext_data
*drv
,
757 int ifindex
, struct nlmsghdr
*h
)
759 if (drv
->ifindex
== ifindex
|| drv
->ifindex2
== ifindex
)
762 if (drv
->if_removed
&& wpa_driver_wext_own_ifname(drv
, h
)) {
763 drv
->ifindex
= if_nametoindex(drv
->ifname
);
764 wpa_printf(MSG_DEBUG
, "WEXT: Update ifindex for a removed "
766 wpa_driver_wext_finish_drv_init(drv
);
774 static void wpa_driver_wext_event_rtm_newlink(struct wpa_driver_wext_data
*drv
,
775 void *ctx
, struct nlmsghdr
*h
,
778 struct ifinfomsg
*ifi
;
779 int attrlen
, nlmsg_len
, rta_len
;
780 struct rtattr
* attr
;
782 if (len
< sizeof(*ifi
))
787 if (!wpa_driver_wext_own_ifindex(drv
, ifi
->ifi_index
, h
)) {
788 wpa_printf(MSG_DEBUG
, "Ignore event for foreign ifindex %d",
793 wpa_printf(MSG_DEBUG
, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
795 drv
->operstate
, ifi
->ifi_flags
,
796 (ifi
->ifi_flags
& IFF_UP
) ? "[UP]" : "",
797 (ifi
->ifi_flags
& IFF_RUNNING
) ? "[RUNNING]" : "",
798 (ifi
->ifi_flags
& IFF_LOWER_UP
) ? "[LOWER_UP]" : "",
799 (ifi
->ifi_flags
& IFF_DORMANT
) ? "[DORMANT]" : "");
801 * Some drivers send the association event before the operup event--in
802 * this case, lifting operstate in wpa_driver_wext_set_operstate()
803 * fails. This will hit us when wpa_supplicant does not need to do
804 * IEEE 802.1X authentication
806 if (drv
->operstate
== 1 &&
807 (ifi
->ifi_flags
& (IFF_LOWER_UP
| IFF_DORMANT
)) == IFF_LOWER_UP
&&
808 !(ifi
->ifi_flags
& IFF_RUNNING
))
809 wpa_driver_wext_send_oper_ifla(drv
, -1, IF_OPER_UP
);
811 nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
813 attrlen
= h
->nlmsg_len
- nlmsg_len
;
817 attr
= (struct rtattr
*) (((char *) ifi
) + nlmsg_len
);
819 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
820 while (RTA_OK(attr
, attrlen
)) {
821 if (attr
->rta_type
== IFLA_WIRELESS
) {
822 wpa_driver_wext_event_wireless(
823 drv
, ctx
, ((char *) attr
) + rta_len
,
824 attr
->rta_len
- rta_len
);
825 } else if (attr
->rta_type
== IFLA_IFNAME
) {
826 wpa_driver_wext_event_link(drv
, ctx
,
827 ((char *) attr
) + rta_len
,
828 attr
->rta_len
- rta_len
, 0);
830 attr
= RTA_NEXT(attr
, attrlen
);
835 static void wpa_driver_wext_event_rtm_dellink(struct wpa_driver_wext_data
*drv
,
836 void *ctx
, struct nlmsghdr
*h
,
839 struct ifinfomsg
*ifi
;
840 int attrlen
, nlmsg_len
, rta_len
;
841 struct rtattr
* attr
;
843 if (len
< sizeof(*ifi
))
848 nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
850 attrlen
= h
->nlmsg_len
- nlmsg_len
;
854 attr
= (struct rtattr
*) (((char *) ifi
) + nlmsg_len
);
856 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
857 while (RTA_OK(attr
, attrlen
)) {
858 if (attr
->rta_type
== IFLA_IFNAME
) {
859 wpa_driver_wext_event_link(drv
, ctx
,
860 ((char *) attr
) + rta_len
,
861 attr
->rta_len
- rta_len
, 1);
863 attr
= RTA_NEXT(attr
, attrlen
);
868 static void wpa_driver_wext_event_receive(int sock
, void *eloop_ctx
,
873 struct sockaddr_nl from
;
879 fromlen
= sizeof(from
);
880 left
= recvfrom(sock
, buf
, sizeof(buf
), MSG_DONTWAIT
,
881 (struct sockaddr
*) &from
, &fromlen
);
883 if (errno
!= EINTR
&& errno
!= EAGAIN
)
884 perror("recvfrom(netlink)");
888 h
= (struct nlmsghdr
*) buf
;
889 while (left
>= (int) sizeof(*h
)) {
893 plen
= len
- sizeof(*h
);
894 if (len
> left
|| plen
< 0) {
895 wpa_printf(MSG_DEBUG
, "Malformed netlink message: "
896 "len=%d left=%d plen=%d",
901 switch (h
->nlmsg_type
) {
903 wpa_driver_wext_event_rtm_newlink(eloop_ctx
, sock_ctx
,
907 wpa_driver_wext_event_rtm_dellink(eloop_ctx
, sock_ctx
,
912 len
= NLMSG_ALIGN(len
);
914 h
= (struct nlmsghdr
*) ((char *) h
+ len
);
918 wpa_printf(MSG_DEBUG
, "%d extra bytes in the end of netlink "
922 if (--max_events
> 0) {
924 * Try to receive all events in one eloop call in order to
925 * limit race condition on cases where AssocInfo event, Assoc
926 * event, and EAPOL frames are received more or less at the
927 * same time. We want to process the event messages first
928 * before starting EAPOL processing.
935 static int wpa_driver_wext_get_ifflags_ifname(struct wpa_driver_wext_data
*drv
,
936 const char *ifname
, int *flags
)
940 os_memset(&ifr
, 0, sizeof(ifr
));
941 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
942 if (ioctl(drv
->ioctl_sock
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
943 perror("ioctl[SIOCGIFFLAGS]");
946 *flags
= ifr
.ifr_flags
& 0xffff;
952 * wpa_driver_wext_get_ifflags - Get interface flags (SIOCGIFFLAGS)
953 * @drv: driver_wext private data
954 * @flags: Pointer to returned flags value
955 * Returns: 0 on success, -1 on failure
957 int wpa_driver_wext_get_ifflags(struct wpa_driver_wext_data
*drv
, int *flags
)
959 return wpa_driver_wext_get_ifflags_ifname(drv
, drv
->ifname
, flags
);
963 static int wpa_driver_wext_set_ifflags_ifname(struct wpa_driver_wext_data
*drv
,
964 const char *ifname
, int flags
)
968 os_memset(&ifr
, 0, sizeof(ifr
));
969 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
970 ifr
.ifr_flags
= flags
& 0xffff;
971 if (ioctl(drv
->ioctl_sock
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
972 perror("SIOCSIFFLAGS");
979 #ifdef CONFIG_CLIENT_MLME
981 static int wpa_driver_prism2_param_set(struct wpa_driver_wext_data
*drv
,
982 int param
, int value
)
987 os_memset(&iwr
, 0, sizeof(iwr
));
988 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
989 i
= (int *) iwr
.u
.name
;
993 return ioctl(drv
->ioctl_sock
, PRISM2_IOCTL_PRISM2_PARAM
, &iwr
);
997 static int wpa_driver_prism2_param_get(struct wpa_driver_wext_data
*drv
,
1003 os_memset(&iwr
, 0, sizeof(iwr
));
1004 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1005 i
= (int *) iwr
.u
.name
;
1008 if (ioctl(drv
->ioctl_sock
, PRISM2_IOCTL_GET_PRISM2_PARAM
, &iwr
) < 0) {
1009 perror("ioctl[PRISM2_IOCTL_GET_PRISM2_PARAM]");
1016 #endif /* CONFIG_CLIENT_MLME */
1020 * wpa_driver_wext_set_ifflags - Set interface flags (SIOCSIFFLAGS)
1021 * @drv: driver_wext private data
1022 * @flags: New value for flags
1023 * Returns: 0 on success, -1 on failure
1025 int wpa_driver_wext_set_ifflags(struct wpa_driver_wext_data
*drv
, int flags
)
1027 return wpa_driver_wext_set_ifflags_ifname(drv
, drv
->ifname
, flags
);
1032 * wpa_driver_wext_init - Initialize WE driver interface
1033 * @ctx: context to be used when calling wpa_supplicant functions,
1034 * e.g., wpa_supplicant_event()
1035 * @ifname: interface name, e.g., wlan0
1036 * Returns: Pointer to private data, %NULL on failure
1038 void * wpa_driver_wext_init(void *ctx
, const char *ifname
)
1041 struct sockaddr_nl local
;
1042 struct wpa_driver_wext_data
*drv
;
1044 drv
= os_zalloc(sizeof(*drv
));
1048 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
1050 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
1051 if (drv
->ioctl_sock
< 0) {
1052 perror("socket(PF_INET,SOCK_DGRAM)");
1057 s
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1059 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1060 close(drv
->ioctl_sock
);
1065 os_memset(&local
, 0, sizeof(local
));
1066 local
.nl_family
= AF_NETLINK
;
1067 local
.nl_groups
= RTMGRP_LINK
;
1068 if (bind(s
, (struct sockaddr
*) &local
, sizeof(local
)) < 0) {
1069 perror("bind(netlink)");
1071 close(drv
->ioctl_sock
);
1076 eloop_register_read_sock(s
, wpa_driver_wext_event_receive
, drv
, ctx
);
1077 drv
->event_sock
= s
;
1079 drv
->mlme_sock
= -1;
1081 wpa_driver_wext_finish_drv_init(drv
);
1087 static void wpa_driver_wext_finish_drv_init(struct wpa_driver_wext_data
*drv
)
1091 if (wpa_driver_wext_get_ifflags(drv
, &flags
) != 0)
1092 printf("Could not get interface '%s' flags\n", drv
->ifname
);
1093 else if (!(flags
& IFF_UP
)) {
1094 if (wpa_driver_wext_set_ifflags(drv
, flags
| IFF_UP
) != 0) {
1095 printf("Could not set interface '%s' UP\n",
1099 * Wait some time to allow driver to initialize before
1100 * starting configuring the driver. This seems to be
1101 * needed at least some drivers that load firmware etc.
1102 * when the interface is set up.
1104 wpa_printf(MSG_DEBUG
, "Interface %s set UP - waiting "
1105 "a second for the driver to complete "
1106 "initialization", drv
->ifname
);
1112 * Make sure that the driver does not have any obsolete PMKID entries.
1114 wpa_driver_wext_flush_pmkid(drv
);
1116 if (wpa_driver_wext_set_mode(drv
, 0) < 0) {
1117 printf("Could not configure driver to use managed mode\n");
1120 wpa_driver_wext_get_range(drv
);
1122 drv
->ifindex
= if_nametoindex(drv
->ifname
);
1124 if (os_strncmp(drv
->ifname
, "wlan", 4) == 0) {
1126 * Host AP driver may use both wlan# and wifi# interface in
1127 * wireless events. Since some of the versions included WE-18
1128 * support, let's add the alternative ifindex also from
1129 * driver_wext.c for the time being. This may be removed at
1130 * some point once it is believed that old versions of the
1131 * driver are not in use anymore.
1133 char ifname2
[IFNAMSIZ
+ 1];
1134 os_strlcpy(ifname2
, drv
->ifname
, sizeof(ifname2
));
1135 os_memcpy(ifname2
, "wifi", 4);
1136 wpa_driver_wext_alternative_ifindex(drv
, ifname2
);
1139 wpa_driver_wext_send_oper_ifla(drv
, 1, IF_OPER_DORMANT
);
1144 * wpa_driver_wext_deinit - Deinitialize WE driver interface
1145 * @priv: Pointer to private wext data from wpa_driver_wext_init()
1147 * Shut down driver interface and processing of driver events. Free
1148 * private data buffer if one was allocated in wpa_driver_wext_init().
1150 void wpa_driver_wext_deinit(void *priv
)
1152 struct wpa_driver_wext_data
*drv
= priv
;
1155 eloop_cancel_timeout(wpa_driver_wext_scan_timeout
, drv
, drv
->ctx
);
1158 * Clear possibly configured driver parameters in order to make it
1159 * easier to use the driver after wpa_supplicant has been terminated.
1161 (void) wpa_driver_wext_set_bssid(drv
,
1162 (u8
*) "\x00\x00\x00\x00\x00\x00");
1164 wpa_driver_wext_send_oper_ifla(priv
, 0, IF_OPER_UP
);
1166 eloop_unregister_read_sock(drv
->event_sock
);
1167 if (drv
->mlme_sock
>= 0)
1168 eloop_unregister_read_sock(drv
->mlme_sock
);
1170 if (wpa_driver_wext_get_ifflags(drv
, &flags
) == 0)
1171 (void) wpa_driver_wext_set_ifflags(drv
, flags
& ~IFF_UP
);
1173 #ifdef CONFIG_CLIENT_MLME
1174 if (drv
->mlmedev
[0]) {
1175 if (wpa_driver_wext_get_ifflags_ifname(drv
, drv
->mlmedev
,
1177 (void) wpa_driver_wext_set_ifflags_ifname(
1178 drv
, drv
->mlmedev
, flags
& ~IFF_UP
);
1179 wpa_driver_prism2_param_set(drv
, PRISM2_PARAM_MGMT_IF
, 0);
1180 wpa_driver_prism2_param_set(drv
, PRISM2_PARAM_USER_SPACE_MLME
,
1183 #endif /* CONFIG_CLIENT_MLME */
1185 close(drv
->event_sock
);
1186 close(drv
->ioctl_sock
);
1187 if (drv
->mlme_sock
>= 0)
1188 close(drv
->mlme_sock
);
1189 os_free(drv
->assoc_req_ies
);
1190 os_free(drv
->assoc_resp_ies
);
1196 * wpa_driver_wext_scan_timeout - Scan timeout to report scan completion
1197 * @eloop_ctx: Unused
1198 * @timeout_ctx: ctx argument given to wpa_driver_wext_init()
1200 * This function can be used as registered timeout when starting a scan to
1201 * generate a scan completed event if the driver does not report this.
1203 void wpa_driver_wext_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
1205 wpa_printf(MSG_DEBUG
, "Scan timeout - try to get results");
1206 wpa_supplicant_event(timeout_ctx
, EVENT_SCAN_RESULTS
, NULL
);
1211 * wpa_driver_wext_scan - Request the driver to initiate scan
1212 * @priv: Pointer to private wext data from wpa_driver_wext_init()
1213 * @ssid: Specific SSID to scan for (ProbeReq) or %NULL to scan for
1214 * all SSIDs (either active scan with broadcast SSID or passive
1216 * @ssid_len: Length of the SSID
1217 * Returns: 0 on success, -1 on failure
1219 int wpa_driver_wext_scan(void *priv
, const u8
*ssid
, size_t ssid_len
)
1221 struct wpa_driver_wext_data
*drv
= priv
;
1223 int ret
= 0, timeout
;
1224 struct iw_scan_req req
;
1226 if (ssid_len
> IW_ESSID_MAX_SIZE
) {
1227 wpa_printf(MSG_DEBUG
, "%s: too long SSID (%lu)",
1228 __FUNCTION__
, (unsigned long) ssid_len
);
1232 os_memset(&iwr
, 0, sizeof(iwr
));
1233 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1235 if (ssid
&& ssid_len
) {
1236 os_memset(&req
, 0, sizeof(req
));
1237 req
.essid_len
= ssid_len
;
1238 req
.bssid
.sa_family
= ARPHRD_ETHER
;
1239 os_memset(req
.bssid
.sa_data
, 0xff, ETH_ALEN
);
1240 os_memcpy(req
.essid
, ssid
, ssid_len
);
1241 iwr
.u
.data
.pointer
= (caddr_t
) &req
;
1242 iwr
.u
.data
.length
= sizeof(req
);
1243 iwr
.u
.data
.flags
= IW_SCAN_THIS_ESSID
;
1246 if (ioctl(drv
->ioctl_sock
, SIOCSIWSCAN
, &iwr
) < 0) {
1247 perror("ioctl[SIOCSIWSCAN]");
1251 /* Not all drivers generate "scan completed" wireless event, so try to
1252 * read results after a timeout. */
1254 if (drv
->scan_complete_events
) {
1256 * The driver seems to deliver SIOCGIWSCAN events to notify
1257 * when scan is complete, so use longer timeout to avoid race
1258 * conditions with scanning and following association request.
1262 wpa_printf(MSG_DEBUG
, "Scan requested (ret=%d) - scan timeout %d "
1263 "seconds", ret
, timeout
);
1264 eloop_cancel_timeout(wpa_driver_wext_scan_timeout
, drv
, drv
->ctx
);
1265 eloop_register_timeout(timeout
, 0, wpa_driver_wext_scan_timeout
, drv
,
1272 static u8
* wpa_driver_wext_giwscan(struct wpa_driver_wext_data
*drv
,
1279 res_buf_len
= IW_SCAN_MAX_DATA
;
1281 res_buf
= os_malloc(res_buf_len
);
1282 if (res_buf
== NULL
)
1284 os_memset(&iwr
, 0, sizeof(iwr
));
1285 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1286 iwr
.u
.data
.pointer
= res_buf
;
1287 iwr
.u
.data
.length
= res_buf_len
;
1289 if (ioctl(drv
->ioctl_sock
, SIOCGIWSCAN
, &iwr
) == 0)
1292 if (errno
== E2BIG
&& res_buf_len
< 100000) {
1296 wpa_printf(MSG_DEBUG
, "Scan results did not fit - "
1297 "trying larger buffer (%lu bytes)",
1298 (unsigned long) res_buf_len
);
1300 perror("ioctl[SIOCGIWSCAN]");
1306 if (iwr
.u
.data
.length
> res_buf_len
) {
1310 *len
= iwr
.u
.data
.length
;
1317 * Data structure for collecting WEXT scan results. This is needed to allow
1318 * the various methods of reporting IEs to be combined into a single IE buffer.
1320 struct wext_scan_data
{
1321 struct wpa_scan_res res
;
1330 static void wext_get_scan_mode(struct iw_event
*iwe
,
1331 struct wext_scan_data
*res
)
1333 if (iwe
->u
.mode
== IW_MODE_ADHOC
)
1334 res
->res
.caps
|= IEEE80211_CAP_IBSS
;
1335 else if (iwe
->u
.mode
== IW_MODE_MASTER
|| iwe
->u
.mode
== IW_MODE_INFRA
)
1336 res
->res
.caps
|= IEEE80211_CAP_ESS
;
1340 static void wext_get_scan_ssid(struct iw_event
*iwe
,
1341 struct wext_scan_data
*res
, char *custom
,
1344 int ssid_len
= iwe
->u
.essid
.length
;
1345 if (custom
+ ssid_len
> end
)
1347 if (iwe
->u
.essid
.flags
&&
1349 ssid_len
<= IW_ESSID_MAX_SIZE
) {
1350 os_memcpy(res
->ssid
, custom
, ssid_len
);
1351 res
->ssid_len
= ssid_len
;
1356 static void wext_get_scan_freq(struct iw_event
*iwe
,
1357 struct wext_scan_data
*res
)
1359 int divi
= 1000000, i
;
1361 if (iwe
->u
.freq
.e
== 0) {
1363 * Some drivers do not report frequency, but a channel.
1364 * Try to map this to frequency by assuming they are using
1365 * IEEE 802.11b/g. But don't overwrite a previously parsed
1366 * frequency if the driver sends both frequency and channel,
1367 * since the driver may be sending an A-band channel that we
1368 * don't handle here.
1374 if (iwe
->u
.freq
.m
>= 1 && iwe
->u
.freq
.m
<= 13) {
1375 res
->res
.freq
= 2407 + 5 * iwe
->u
.freq
.m
;
1377 } else if (iwe
->u
.freq
.m
== 14) {
1378 res
->res
.freq
= 2484;
1383 if (iwe
->u
.freq
.e
> 6) {
1384 wpa_printf(MSG_DEBUG
, "Invalid freq in scan results (BSSID="
1385 MACSTR
" m=%d e=%d)",
1386 MAC2STR(res
->res
.bssid
), iwe
->u
.freq
.m
,
1391 for (i
= 0; i
< iwe
->u
.freq
.e
; i
++)
1393 res
->res
.freq
= iwe
->u
.freq
.m
/ divi
;
1397 static void wext_get_scan_qual(struct iw_event
*iwe
,
1398 struct wext_scan_data
*res
)
1400 res
->res
.qual
= iwe
->u
.qual
.qual
;
1401 res
->res
.noise
= iwe
->u
.qual
.noise
;
1402 res
->res
.level
= iwe
->u
.qual
.level
;
1406 static void wext_get_scan_encode(struct iw_event
*iwe
,
1407 struct wext_scan_data
*res
)
1409 if (!(iwe
->u
.data
.flags
& IW_ENCODE_DISABLED
))
1410 res
->res
.caps
|= IEEE80211_CAP_PRIVACY
;
1414 static void wext_get_scan_rate(struct iw_event
*iwe
,
1415 struct wext_scan_data
*res
, char *pos
,
1419 char *custom
= pos
+ IW_EV_LCP_LEN
;
1424 if (custom
+ clen
> end
)
1427 while (((ssize_t
) clen
) >= (ssize_t
) sizeof(struct iw_param
)) {
1428 /* Note: may be misaligned, make a local, aligned copy */
1429 os_memcpy(&p
, custom
, sizeof(struct iw_param
));
1430 if (p
.value
> maxrate
)
1432 clen
-= sizeof(struct iw_param
);
1433 custom
+= sizeof(struct iw_param
);
1436 /* Convert the maxrate from WE-style (b/s units) to
1437 * 802.11 rates (500000 b/s units).
1439 res
->maxrate
= maxrate
/ 500000;
1443 static void wext_get_scan_iwevgenie(struct iw_event
*iwe
,
1444 struct wext_scan_data
*res
, char *custom
,
1447 char *genie
, *gpos
, *gend
;
1450 gpos
= genie
= custom
;
1451 gend
= genie
+ iwe
->u
.data
.length
;
1453 wpa_printf(MSG_INFO
, "IWEVGENIE overflow");
1457 tmp
= os_realloc(res
->ie
, res
->ie_len
+ gend
- gpos
);
1460 os_memcpy(tmp
+ res
->ie_len
, gpos
, gend
- gpos
);
1462 res
->ie_len
+= gend
- gpos
;
1466 static void wext_get_scan_custom(struct iw_event
*iwe
,
1467 struct wext_scan_data
*res
, char *custom
,
1473 clen
= iwe
->u
.data
.length
;
1474 if (custom
+ clen
> end
)
1477 if (clen
> 7 && os_strncmp(custom
, "wpa_ie=", 7) == 0) {
1481 bytes
= custom
+ clen
- spos
;
1485 tmp
= os_realloc(res
->ie
, res
->ie_len
+ bytes
);
1488 hexstr2bin(spos
, tmp
+ res
->ie_len
, bytes
);
1490 res
->ie_len
+= bytes
;
1491 } else if (clen
> 7 && os_strncmp(custom
, "rsn_ie=", 7) == 0) {
1495 bytes
= custom
+ clen
- spos
;
1499 tmp
= os_realloc(res
->ie
, res
->ie_len
+ bytes
);
1502 hexstr2bin(spos
, tmp
+ res
->ie_len
, bytes
);
1504 res
->ie_len
+= bytes
;
1505 } else if (clen
> 4 && os_strncmp(custom
, "tsf=", 4) == 0) {
1510 bytes
= custom
+ clen
- spos
;
1512 wpa_printf(MSG_INFO
, "Invalid TSF length (%d)", bytes
);
1516 hexstr2bin(spos
, bin
, bytes
);
1517 res
->res
.tsf
+= WPA_GET_BE64(bin
);
1522 static int wext_19_iw_point(struct wpa_driver_wext_data
*drv
, u16 cmd
)
1524 return drv
->we_version_compiled
> 18 &&
1525 (cmd
== SIOCGIWESSID
|| cmd
== SIOCGIWENCODE
||
1526 cmd
== IWEVGENIE
|| cmd
== IWEVCUSTOM
);
1530 static void wpa_driver_wext_add_scan_entry(struct wpa_scan_results
*res
,
1531 struct wext_scan_data
*data
)
1533 struct wpa_scan_res
**tmp
;
1534 struct wpa_scan_res
*r
;
1536 u8
*pos
, *end
, *ssid_ie
= NULL
, *rate_ie
= NULL
;
1538 /* Figure out whether we need to fake any IEs */
1540 end
= pos
+ data
->ie_len
;
1541 while (pos
&& pos
+ 1 < end
) {
1542 if (pos
+ 2 + pos
[1] > end
)
1544 if (pos
[0] == WLAN_EID_SSID
)
1546 else if (pos
[0] == WLAN_EID_SUPP_RATES
)
1548 else if (pos
[0] == WLAN_EID_EXT_SUPP_RATES
)
1554 if (ssid_ie
== NULL
)
1555 extra_len
+= 2 + data
->ssid_len
;
1556 if (rate_ie
== NULL
&& data
->maxrate
)
1559 r
= os_zalloc(sizeof(*r
) + extra_len
+ data
->ie_len
);
1562 os_memcpy(r
, &data
->res
, sizeof(*r
));
1563 r
->ie_len
= extra_len
+ data
->ie_len
;
1564 pos
= (u8
*) (r
+ 1);
1565 if (ssid_ie
== NULL
) {
1567 * Generate a fake SSID IE since the driver did not report
1570 *pos
++ = WLAN_EID_SSID
;
1571 *pos
++ = data
->ssid_len
;
1572 os_memcpy(pos
, data
->ssid
, data
->ssid_len
);
1573 pos
+= data
->ssid_len
;
1575 if (rate_ie
== NULL
&& data
->maxrate
) {
1577 * Generate a fake Supported Rates IE since the driver did not
1578 * report a full IE list.
1580 *pos
++ = WLAN_EID_SUPP_RATES
;
1582 *pos
++ = data
->maxrate
;
1585 os_memcpy(pos
, data
->ie
, data
->ie_len
);
1587 tmp
= os_realloc(res
->res
,
1588 (res
->num
+ 1) * sizeof(struct wpa_scan_res
*));
1593 tmp
[res
->num
++] = r
;
1599 * wpa_driver_wext_get_scan_results - Fetch the latest scan results
1600 * @priv: Pointer to private wext data from wpa_driver_wext_init()
1601 * Returns: Scan results on success, -1 on failure
1603 struct wpa_scan_results
* wpa_driver_wext_get_scan_results(void *priv
)
1605 struct wpa_driver_wext_data
*drv
= priv
;
1606 size_t ap_num
= 0, len
;
1609 struct iw_event iwe_buf
, *iwe
= &iwe_buf
;
1610 char *pos
, *end
, *custom
;
1611 struct wpa_scan_results
*res
;
1612 struct wext_scan_data data
;
1614 res_buf
= wpa_driver_wext_giwscan(drv
, &len
);
1615 if (res_buf
== NULL
)
1621 res
= os_zalloc(sizeof(*res
));
1627 pos
= (char *) res_buf
;
1628 end
= (char *) res_buf
+ len
;
1629 os_memset(&data
, 0, sizeof(data
));
1631 while (pos
+ IW_EV_LCP_LEN
<= end
) {
1632 /* Event data may be unaligned, so make a local, aligned copy
1633 * before processing. */
1634 os_memcpy(&iwe_buf
, pos
, IW_EV_LCP_LEN
);
1635 if (iwe
->len
<= IW_EV_LCP_LEN
)
1638 custom
= pos
+ IW_EV_POINT_LEN
;
1639 if (wext_19_iw_point(drv
, iwe
->cmd
)) {
1640 /* WE-19 removed the pointer from struct iw_point */
1641 char *dpos
= (char *) &iwe_buf
.u
.data
.length
;
1642 int dlen
= dpos
- (char *) &iwe_buf
;
1643 os_memcpy(dpos
, pos
+ IW_EV_LCP_LEN
,
1644 sizeof(struct iw_event
) - dlen
);
1646 os_memcpy(&iwe_buf
, pos
, sizeof(struct iw_event
));
1647 custom
+= IW_EV_POINT_OFF
;
1653 wpa_driver_wext_add_scan_entry(res
, &data
);
1656 os_memset(&data
, 0, sizeof(data
));
1657 os_memcpy(data
.res
.bssid
,
1658 iwe
->u
.ap_addr
.sa_data
, ETH_ALEN
);
1661 wext_get_scan_mode(iwe
, &data
);
1664 wext_get_scan_ssid(iwe
, &data
, custom
, end
);
1667 wext_get_scan_freq(iwe
, &data
);
1670 wext_get_scan_qual(iwe
, &data
);
1673 wext_get_scan_encode(iwe
, &data
);
1676 wext_get_scan_rate(iwe
, &data
, pos
, end
);
1679 wext_get_scan_iwevgenie(iwe
, &data
, custom
, end
);
1682 wext_get_scan_custom(iwe
, &data
, custom
, end
);
1691 wpa_driver_wext_add_scan_entry(res
, &data
);
1694 wpa_printf(MSG_DEBUG
, "Received %lu bytes of scan results (%lu BSSes)",
1695 (unsigned long) len
, (unsigned long) res
->num
);
1701 static int wpa_driver_wext_get_range(void *priv
)
1703 struct wpa_driver_wext_data
*drv
= priv
;
1704 struct iw_range
*range
;
1710 * Use larger buffer than struct iw_range in order to allow the
1711 * structure to grow in the future.
1713 buflen
= sizeof(struct iw_range
) + 500;
1714 range
= os_zalloc(buflen
);
1718 os_memset(&iwr
, 0, sizeof(iwr
));
1719 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1720 iwr
.u
.data
.pointer
= (caddr_t
) range
;
1721 iwr
.u
.data
.length
= buflen
;
1723 minlen
= ((char *) &range
->enc_capa
) - (char *) range
+
1724 sizeof(range
->enc_capa
);
1726 if (ioctl(drv
->ioctl_sock
, SIOCGIWRANGE
, &iwr
) < 0) {
1727 perror("ioctl[SIOCGIWRANGE]");
1730 } else if (iwr
.u
.data
.length
>= minlen
&&
1731 range
->we_version_compiled
>= 18) {
1732 wpa_printf(MSG_DEBUG
, "SIOCGIWRANGE: WE(compiled)=%d "
1733 "WE(source)=%d enc_capa=0x%x",
1734 range
->we_version_compiled
,
1735 range
->we_version_source
,
1737 drv
->has_capability
= 1;
1738 drv
->we_version_compiled
= range
->we_version_compiled
;
1739 if (range
->enc_capa
& IW_ENC_CAPA_WPA
) {
1740 drv
->capa
.key_mgmt
|= WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
1741 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
;
1743 if (range
->enc_capa
& IW_ENC_CAPA_WPA2
) {
1744 drv
->capa
.key_mgmt
|= WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
1745 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
;
1747 drv
->capa
.enc
|= WPA_DRIVER_CAPA_ENC_WEP40
|
1748 WPA_DRIVER_CAPA_ENC_WEP104
;
1749 if (range
->enc_capa
& IW_ENC_CAPA_CIPHER_TKIP
)
1750 drv
->capa
.enc
|= WPA_DRIVER_CAPA_ENC_TKIP
;
1751 if (range
->enc_capa
& IW_ENC_CAPA_CIPHER_CCMP
)
1752 drv
->capa
.enc
|= WPA_DRIVER_CAPA_ENC_CCMP
;
1753 if (range
->enc_capa
& IW_ENC_CAPA_4WAY_HANDSHAKE
)
1754 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_4WAY_HANDSHAKE
;
1756 wpa_printf(MSG_DEBUG
, " capabilities: key_mgmt 0x%x enc 0x%x "
1758 drv
->capa
.key_mgmt
, drv
->capa
.enc
, drv
->capa
.flags
);
1760 wpa_printf(MSG_DEBUG
, "SIOCGIWRANGE: too old (short) data - "
1761 "assuming WPA is not supported");
1769 static int wpa_driver_wext_set_wpa(void *priv
, int enabled
)
1771 struct wpa_driver_wext_data
*drv
= priv
;
1772 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
1774 return wpa_driver_wext_set_auth_param(drv
, IW_AUTH_WPA_ENABLED
,
1779 static int wpa_driver_wext_set_psk(struct wpa_driver_wext_data
*drv
,
1782 struct iw_encode_ext
*ext
;
1786 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
1788 if (!(drv
->capa
.flags
& WPA_DRIVER_FLAGS_4WAY_HANDSHAKE
))
1794 os_memset(&iwr
, 0, sizeof(iwr
));
1795 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1797 ext
= os_zalloc(sizeof(*ext
) + PMK_LEN
);
1801 iwr
.u
.encoding
.pointer
= (caddr_t
) ext
;
1802 iwr
.u
.encoding
.length
= sizeof(*ext
) + PMK_LEN
;
1803 ext
->key_len
= PMK_LEN
;
1804 os_memcpy(&ext
->key
, psk
, ext
->key_len
);
1805 ext
->alg
= IW_ENCODE_ALG_PMK
;
1807 ret
= ioctl(drv
->ioctl_sock
, SIOCSIWENCODEEXT
, &iwr
);
1809 perror("ioctl[SIOCSIWENCODEEXT] PMK");
1816 static int wpa_driver_wext_set_key_ext(void *priv
, wpa_alg alg
,
1817 const u8
*addr
, int key_idx
,
1818 int set_tx
, const u8
*seq
,
1820 const u8
*key
, size_t key_len
)
1822 struct wpa_driver_wext_data
*drv
= priv
;
1825 struct iw_encode_ext
*ext
;
1827 if (seq_len
> IW_ENCODE_SEQ_MAX_SIZE
) {
1828 wpa_printf(MSG_DEBUG
, "%s: Invalid seq_len %lu",
1829 __FUNCTION__
, (unsigned long) seq_len
);
1833 ext
= os_zalloc(sizeof(*ext
) + key_len
);
1836 os_memset(&iwr
, 0, sizeof(iwr
));
1837 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1838 iwr
.u
.encoding
.flags
= key_idx
+ 1;
1839 if (alg
== WPA_ALG_NONE
)
1840 iwr
.u
.encoding
.flags
|= IW_ENCODE_DISABLED
;
1841 iwr
.u
.encoding
.pointer
= (caddr_t
) ext
;
1842 iwr
.u
.encoding
.length
= sizeof(*ext
) + key_len
;
1845 os_memcmp(addr
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) == 0)
1846 ext
->ext_flags
|= IW_ENCODE_EXT_GROUP_KEY
;
1848 ext
->ext_flags
|= IW_ENCODE_EXT_SET_TX_KEY
;
1850 ext
->addr
.sa_family
= ARPHRD_ETHER
;
1852 os_memcpy(ext
->addr
.sa_data
, addr
, ETH_ALEN
);
1854 os_memset(ext
->addr
.sa_data
, 0xff, ETH_ALEN
);
1855 if (key
&& key_len
) {
1856 os_memcpy(ext
+ 1, key
, key_len
);
1857 ext
->key_len
= key_len
;
1861 ext
->alg
= IW_ENCODE_ALG_NONE
;
1864 ext
->alg
= IW_ENCODE_ALG_WEP
;
1867 ext
->alg
= IW_ENCODE_ALG_TKIP
;
1870 ext
->alg
= IW_ENCODE_ALG_CCMP
;
1873 ext
->alg
= IW_ENCODE_ALG_PMK
;
1875 #ifdef WEXT_MFP_PENDING
1876 #ifdef CONFIG_IEEE80211W
1878 ext
->alg
= IW_ENCODE_ALG_AES_CMAC
;
1880 #endif /* CONFIG_IEEE80211W */
1881 #endif /* WEXT_MFP_PENDING */
1883 wpa_printf(MSG_DEBUG
, "%s: Unknown algorithm %d",
1889 if (seq
&& seq_len
) {
1890 ext
->ext_flags
|= IW_ENCODE_EXT_RX_SEQ_VALID
;
1891 os_memcpy(ext
->rx_seq
, seq
, seq_len
);
1894 if (ioctl(drv
->ioctl_sock
, SIOCSIWENCODEEXT
, &iwr
) < 0) {
1895 ret
= errno
== EOPNOTSUPP
? -2 : -1;
1896 if (errno
== ENODEV
) {
1898 * ndiswrapper seems to be returning incorrect error
1903 perror("ioctl[SIOCSIWENCODEEXT]");
1912 * wpa_driver_wext_set_key - Configure encryption key
1913 * @priv: Pointer to private wext data from wpa_driver_wext_init()
1914 * @priv: Private driver interface data
1915 * @alg: Encryption algorithm (%WPA_ALG_NONE, %WPA_ALG_WEP,
1916 * %WPA_ALG_TKIP, %WPA_ALG_CCMP); %WPA_ALG_NONE clears the key.
1917 * @addr: Address of the peer STA or ff:ff:ff:ff:ff:ff for
1918 * broadcast/default keys
1919 * @key_idx: key index (0..3), usually 0 for unicast keys
1920 * @set_tx: Configure this key as the default Tx key (only used when
1921 * driver does not support separate unicast/individual key
1922 * @seq: Sequence number/packet number, seq_len octets, the next
1923 * packet number to be used for in replay protection; configured
1924 * for Rx keys (in most cases, this is only used with broadcast
1925 * keys and set to zero for unicast keys)
1926 * @seq_len: Length of the seq, depends on the algorithm:
1927 * TKIP: 6 octets, CCMP: 6 octets
1928 * @key: Key buffer; TKIP: 16-byte temporal key, 8-byte Tx Mic key,
1930 * @key_len: Length of the key buffer in octets (WEP: 5 or 13,
1931 * TKIP: 32, CCMP: 16)
1932 * Returns: 0 on success, -1 on failure
1934 * This function uses SIOCSIWENCODEEXT by default, but tries to use
1935 * SIOCSIWENCODE if the extended ioctl fails when configuring a WEP key.
1937 int wpa_driver_wext_set_key(void *priv
, wpa_alg alg
,
1938 const u8
*addr
, int key_idx
,
1939 int set_tx
, const u8
*seq
, size_t seq_len
,
1940 const u8
*key
, size_t key_len
)
1942 struct wpa_driver_wext_data
*drv
= priv
;
1946 wpa_printf(MSG_DEBUG
, "%s: alg=%d key_idx=%d set_tx=%d seq_len=%lu "
1948 __FUNCTION__
, alg
, key_idx
, set_tx
,
1949 (unsigned long) seq_len
, (unsigned long) key_len
);
1951 ret
= wpa_driver_wext_set_key_ext(drv
, alg
, addr
, key_idx
, set_tx
,
1952 seq
, seq_len
, key
, key_len
);
1957 (alg
== WPA_ALG_NONE
|| alg
== WPA_ALG_WEP
)) {
1958 wpa_printf(MSG_DEBUG
, "Driver did not support "
1959 "SIOCSIWENCODEEXT, trying SIOCSIWENCODE");
1962 wpa_printf(MSG_DEBUG
, "Driver did not support "
1963 "SIOCSIWENCODEEXT");
1967 os_memset(&iwr
, 0, sizeof(iwr
));
1968 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1969 iwr
.u
.encoding
.flags
= key_idx
+ 1;
1970 if (alg
== WPA_ALG_NONE
)
1971 iwr
.u
.encoding
.flags
|= IW_ENCODE_DISABLED
;
1972 iwr
.u
.encoding
.pointer
= (caddr_t
) key
;
1973 iwr
.u
.encoding
.length
= key_len
;
1975 if (ioctl(drv
->ioctl_sock
, SIOCSIWENCODE
, &iwr
) < 0) {
1976 perror("ioctl[SIOCSIWENCODE]");
1980 if (set_tx
&& alg
!= WPA_ALG_NONE
) {
1981 os_memset(&iwr
, 0, sizeof(iwr
));
1982 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
1983 iwr
.u
.encoding
.flags
= key_idx
+ 1;
1984 iwr
.u
.encoding
.pointer
= (caddr_t
) NULL
;
1985 iwr
.u
.encoding
.length
= 0;
1986 if (ioctl(drv
->ioctl_sock
, SIOCSIWENCODE
, &iwr
) < 0) {
1987 perror("ioctl[SIOCSIWENCODE] (set_tx)");
1996 static int wpa_driver_wext_set_countermeasures(void *priv
,
1999 struct wpa_driver_wext_data
*drv
= priv
;
2000 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
2001 return wpa_driver_wext_set_auth_param(drv
,
2002 IW_AUTH_TKIP_COUNTERMEASURES
,
2007 static int wpa_driver_wext_set_drop_unencrypted(void *priv
,
2010 struct wpa_driver_wext_data
*drv
= priv
;
2011 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
2012 drv
->use_crypt
= enabled
;
2013 return wpa_driver_wext_set_auth_param(drv
, IW_AUTH_DROP_UNENCRYPTED
,
2018 static int wpa_driver_wext_mlme(struct wpa_driver_wext_data
*drv
,
2019 const u8
*addr
, int cmd
, int reason_code
)
2022 struct iw_mlme mlme
;
2025 os_memset(&iwr
, 0, sizeof(iwr
));
2026 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
2027 os_memset(&mlme
, 0, sizeof(mlme
));
2029 mlme
.reason_code
= reason_code
;
2030 mlme
.addr
.sa_family
= ARPHRD_ETHER
;
2031 os_memcpy(mlme
.addr
.sa_data
, addr
, ETH_ALEN
);
2032 iwr
.u
.data
.pointer
= (caddr_t
) &mlme
;
2033 iwr
.u
.data
.length
= sizeof(mlme
);
2035 if (ioctl(drv
->ioctl_sock
, SIOCSIWMLME
, &iwr
) < 0) {
2036 perror("ioctl[SIOCSIWMLME]");
2044 static int wpa_driver_wext_deauthenticate(void *priv
, const u8
*addr
,
2047 struct wpa_driver_wext_data
*drv
= priv
;
2048 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
2049 return wpa_driver_wext_mlme(drv
, addr
, IW_MLME_DEAUTH
, reason_code
);
2053 static int wpa_driver_wext_disassociate(void *priv
, const u8
*addr
,
2056 struct wpa_driver_wext_data
*drv
= priv
;
2057 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
2058 return wpa_driver_wext_mlme(drv
, addr
, IW_MLME_DISASSOC
,
2063 static int wpa_driver_wext_set_gen_ie(void *priv
, const u8
*ie
,
2066 struct wpa_driver_wext_data
*drv
= priv
;
2070 os_memset(&iwr
, 0, sizeof(iwr
));
2071 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
2072 iwr
.u
.data
.pointer
= (caddr_t
) ie
;
2073 iwr
.u
.data
.length
= ie_len
;
2075 if (ioctl(drv
->ioctl_sock
, SIOCSIWGENIE
, &iwr
) < 0) {
2076 perror("ioctl[SIOCSIWGENIE]");
2084 int wpa_driver_wext_cipher2wext(int cipher
)
2088 return IW_AUTH_CIPHER_NONE
;
2090 return IW_AUTH_CIPHER_WEP40
;
2092 return IW_AUTH_CIPHER_TKIP
;
2094 return IW_AUTH_CIPHER_CCMP
;
2096 return IW_AUTH_CIPHER_WEP104
;
2103 int wpa_driver_wext_keymgmt2wext(int keymgmt
)
2106 case KEY_MGMT_802_1X
:
2107 case KEY_MGMT_802_1X_NO_WPA
:
2108 return IW_AUTH_KEY_MGMT_802_1X
;
2110 return IW_AUTH_KEY_MGMT_PSK
;
2118 wpa_driver_wext_auth_alg_fallback(struct wpa_driver_wext_data
*drv
,
2119 struct wpa_driver_associate_params
*params
)
2124 wpa_printf(MSG_DEBUG
, "WEXT: Driver did not support "
2125 "SIOCSIWAUTH for AUTH_ALG, trying SIOCSIWENCODE");
2127 os_memset(&iwr
, 0, sizeof(iwr
));
2128 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
2129 /* Just changing mode, not actual keys */
2130 iwr
.u
.encoding
.flags
= 0;
2131 iwr
.u
.encoding
.pointer
= (caddr_t
) NULL
;
2132 iwr
.u
.encoding
.length
= 0;
2135 * Note: IW_ENCODE_{OPEN,RESTRICTED} can be interpreted to mean two
2136 * different things. Here they are used to indicate Open System vs.
2137 * Shared Key authentication algorithm. However, some drivers may use
2138 * them to select between open/restricted WEP encrypted (open = allow
2139 * both unencrypted and encrypted frames; restricted = only allow
2140 * encrypted frames).
2143 if (!drv
->use_crypt
) {
2144 iwr
.u
.encoding
.flags
|= IW_ENCODE_DISABLED
;
2146 if (params
->auth_alg
& AUTH_ALG_OPEN_SYSTEM
)
2147 iwr
.u
.encoding
.flags
|= IW_ENCODE_OPEN
;
2148 if (params
->auth_alg
& AUTH_ALG_SHARED_KEY
)
2149 iwr
.u
.encoding
.flags
|= IW_ENCODE_RESTRICTED
;
2152 if (ioctl(drv
->ioctl_sock
, SIOCSIWENCODE
, &iwr
) < 0) {
2153 perror("ioctl[SIOCSIWENCODE]");
2161 int wpa_driver_wext_associate(void *priv
,
2162 struct wpa_driver_associate_params
*params
)
2164 struct wpa_driver_wext_data
*drv
= priv
;
2166 int allow_unencrypted_eapol
;
2169 wpa_printf(MSG_DEBUG
, "%s", __FUNCTION__
);
2172 * If the driver did not support SIOCSIWAUTH, fallback to
2173 * SIOCSIWENCODE here.
2175 if (drv
->auth_alg_fallback
&&
2176 wpa_driver_wext_auth_alg_fallback(drv
, params
) < 0)
2179 if (!params
->bssid
&&
2180 wpa_driver_wext_set_bssid(drv
, NULL
) < 0)
2183 if (wpa_driver_wext_set_mode(drv
, params
->mode
) < 0)
2185 /* TODO: should consider getting wpa version and cipher/key_mgmt suites
2186 * from configuration, not from here, where only the selected suite is
2188 if (wpa_driver_wext_set_gen_ie(drv
, params
->wpa_ie
, params
->wpa_ie_len
)
2191 if (params
->wpa_ie
== NULL
|| params
->wpa_ie_len
== 0)
2192 value
= IW_AUTH_WPA_VERSION_DISABLED
;
2193 else if (params
->wpa_ie
[0] == WLAN_EID_RSN
)
2194 value
= IW_AUTH_WPA_VERSION_WPA2
;
2196 value
= IW_AUTH_WPA_VERSION_WPA
;
2197 if (wpa_driver_wext_set_auth_param(drv
,
2198 IW_AUTH_WPA_VERSION
, value
) < 0)
2200 value
= wpa_driver_wext_cipher2wext(params
->pairwise_suite
);
2201 if (wpa_driver_wext_set_auth_param(drv
,
2202 IW_AUTH_CIPHER_PAIRWISE
, value
) < 0)
2204 value
= wpa_driver_wext_cipher2wext(params
->group_suite
);
2205 if (wpa_driver_wext_set_auth_param(drv
,
2206 IW_AUTH_CIPHER_GROUP
, value
) < 0)
2208 value
= wpa_driver_wext_keymgmt2wext(params
->key_mgmt_suite
);
2209 if (wpa_driver_wext_set_auth_param(drv
,
2210 IW_AUTH_KEY_MGMT
, value
) < 0)
2212 value
= params
->key_mgmt_suite
!= KEY_MGMT_NONE
||
2213 params
->pairwise_suite
!= CIPHER_NONE
||
2214 params
->group_suite
!= CIPHER_NONE
||
2216 if (wpa_driver_wext_set_auth_param(drv
,
2217 IW_AUTH_PRIVACY_INVOKED
, value
) < 0)
2220 /* Allow unencrypted EAPOL messages even if pairwise keys are set when
2221 * not using WPA. IEEE 802.1X specifies that these frames are not
2222 * encrypted, but WPA encrypts them when pairwise keys are in use. */
2223 if (params
->key_mgmt_suite
== KEY_MGMT_802_1X
||
2224 params
->key_mgmt_suite
== KEY_MGMT_PSK
)
2225 allow_unencrypted_eapol
= 0;
2227 allow_unencrypted_eapol
= 1;
2229 if (wpa_driver_wext_set_psk(drv
, params
->psk
) < 0)
2231 if (wpa_driver_wext_set_auth_param(drv
,
2232 IW_AUTH_RX_UNENCRYPTED_EAPOL
,
2233 allow_unencrypted_eapol
) < 0)
2235 #ifdef WEXT_MFP_PENDING
2236 #ifdef CONFIG_IEEE80211W
2237 switch (params
->mgmt_frame_protection
) {
2238 case NO_MGMT_FRAME_PROTECTION
:
2239 value
= IW_AUTH_MFP_DISABLED
;
2241 case MGMT_FRAME_PROTECTION_OPTIONAL
:
2242 value
= IW_AUTH_MFP_OPTIONAL
;
2244 case MGMT_FRAME_PROTECTION_REQUIRED
:
2245 value
= IW_AUTH_MFP_REQUIRED
;
2248 if (wpa_driver_wext_set_auth_param(drv
, IW_AUTH_MFP
, value
) < 0)
2250 #endif /* CONFIG_IEEE80211W */
2251 #endif /* WEXT_MFP_PENDING */
2252 if (params
->freq
&& wpa_driver_wext_set_freq(drv
, params
->freq
) < 0)
2254 if (wpa_driver_wext_set_ssid(drv
, params
->ssid
, params
->ssid_len
) < 0)
2256 if (params
->bssid
&&
2257 wpa_driver_wext_set_bssid(drv
, params
->bssid
) < 0)
2264 static int wpa_driver_wext_set_auth_alg(void *priv
, int auth_alg
)
2266 struct wpa_driver_wext_data
*drv
= priv
;
2269 if (auth_alg
& AUTH_ALG_OPEN_SYSTEM
)
2270 algs
|= IW_AUTH_ALG_OPEN_SYSTEM
;
2271 if (auth_alg
& AUTH_ALG_SHARED_KEY
)
2272 algs
|= IW_AUTH_ALG_SHARED_KEY
;
2273 if (auth_alg
& AUTH_ALG_LEAP
)
2274 algs
|= IW_AUTH_ALG_LEAP
;
2276 /* at least one algorithm should be set */
2277 algs
= IW_AUTH_ALG_OPEN_SYSTEM
;
2280 res
= wpa_driver_wext_set_auth_param(drv
, IW_AUTH_80211_AUTH_ALG
,
2282 drv
->auth_alg_fallback
= res
== -2;
2288 * wpa_driver_wext_set_mode - Set wireless mode (infra/adhoc), SIOCSIWMODE
2289 * @priv: Pointer to private wext data from wpa_driver_wext_init()
2290 * @mode: 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
2291 * Returns: 0 on success, -1 on failure
2293 int wpa_driver_wext_set_mode(void *priv
, int mode
)
2295 struct wpa_driver_wext_data
*drv
= priv
;
2297 int ret
= -1, flags
;
2298 unsigned int new_mode
= mode
? IW_MODE_ADHOC
: IW_MODE_INFRA
;
2300 os_memset(&iwr
, 0, sizeof(iwr
));
2301 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
2302 iwr
.u
.mode
= new_mode
;
2303 if (ioctl(drv
->ioctl_sock
, SIOCSIWMODE
, &iwr
) == 0) {
2308 if (errno
!= EBUSY
) {
2309 perror("ioctl[SIOCSIWMODE]");
2313 /* mac80211 doesn't allow mode changes while the device is up, so if
2314 * the device isn't in the mode we're about to change to, take device
2315 * down, try to set the mode again, and bring it back up.
2317 if (ioctl(drv
->ioctl_sock
, SIOCGIWMODE
, &iwr
) < 0) {
2318 perror("ioctl[SIOCGIWMODE]");
2322 if (iwr
.u
.mode
== new_mode
) {
2327 if (wpa_driver_wext_get_ifflags(drv
, &flags
) == 0) {
2328 (void) wpa_driver_wext_set_ifflags(drv
, flags
& ~IFF_UP
);
2330 /* Try to set the mode again while the interface is down */
2331 iwr
.u
.mode
= new_mode
;
2332 if (ioctl(drv
->ioctl_sock
, SIOCSIWMODE
, &iwr
) < 0)
2333 perror("ioctl[SIOCSIWMODE]");
2337 /* Ignore return value of get_ifflags to ensure that the device
2338 * is always up like it was before this function was called.
2340 (void) wpa_driver_wext_get_ifflags(drv
, &flags
);
2341 (void) wpa_driver_wext_set_ifflags(drv
, flags
| IFF_UP
);
2349 static int wpa_driver_wext_pmksa(struct wpa_driver_wext_data
*drv
,
2350 u32 cmd
, const u8
*bssid
, const u8
*pmkid
)
2353 struct iw_pmksa pmksa
;
2356 os_memset(&iwr
, 0, sizeof(iwr
));
2357 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
2358 os_memset(&pmksa
, 0, sizeof(pmksa
));
2360 pmksa
.bssid
.sa_family
= ARPHRD_ETHER
;
2362 os_memcpy(pmksa
.bssid
.sa_data
, bssid
, ETH_ALEN
);
2364 os_memcpy(pmksa
.pmkid
, pmkid
, IW_PMKID_LEN
);
2365 iwr
.u
.data
.pointer
= (caddr_t
) &pmksa
;
2366 iwr
.u
.data
.length
= sizeof(pmksa
);
2368 if (ioctl(drv
->ioctl_sock
, SIOCSIWPMKSA
, &iwr
) < 0) {
2369 if (errno
!= EOPNOTSUPP
)
2370 perror("ioctl[SIOCSIWPMKSA]");
2378 static int wpa_driver_wext_add_pmkid(void *priv
, const u8
*bssid
,
2381 struct wpa_driver_wext_data
*drv
= priv
;
2382 return wpa_driver_wext_pmksa(drv
, IW_PMKSA_ADD
, bssid
, pmkid
);
2386 static int wpa_driver_wext_remove_pmkid(void *priv
, const u8
*bssid
,
2389 struct wpa_driver_wext_data
*drv
= priv
;
2390 return wpa_driver_wext_pmksa(drv
, IW_PMKSA_REMOVE
, bssid
, pmkid
);
2394 static int wpa_driver_wext_flush_pmkid(void *priv
)
2396 struct wpa_driver_wext_data
*drv
= priv
;
2397 return wpa_driver_wext_pmksa(drv
, IW_PMKSA_FLUSH
, NULL
, NULL
);
2401 int wpa_driver_wext_get_capa(void *priv
, struct wpa_driver_capa
*capa
)
2403 struct wpa_driver_wext_data
*drv
= priv
;
2404 if (!drv
->has_capability
)
2406 os_memcpy(capa
, &drv
->capa
, sizeof(*capa
));
2411 int wpa_driver_wext_alternative_ifindex(struct wpa_driver_wext_data
*drv
,
2414 if (ifname
== NULL
) {
2419 drv
->ifindex2
= if_nametoindex(ifname
);
2420 if (drv
->ifindex2
<= 0)
2423 wpa_printf(MSG_DEBUG
, "Added alternative ifindex %d (%s) for "
2424 "wireless events", drv
->ifindex2
, ifname
);
2430 int wpa_driver_wext_set_operstate(void *priv
, int state
)
2432 struct wpa_driver_wext_data
*drv
= priv
;
2434 wpa_printf(MSG_DEBUG
, "%s: operstate %d->%d (%s)",
2435 __func__
, drv
->operstate
, state
, state
? "UP" : "DORMANT");
2436 drv
->operstate
= state
;
2437 return wpa_driver_wext_send_oper_ifla(
2438 drv
, -1, state
? IF_OPER_UP
: IF_OPER_DORMANT
);
2442 #ifdef CONFIG_CLIENT_MLME
2443 static int hostapd_ioctl(struct wpa_driver_wext_data
*drv
,
2444 struct prism2_hostapd_param
*param
, int len
)
2448 os_memset(&iwr
, 0, sizeof(iwr
));
2449 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
2450 iwr
.u
.data
.pointer
= (caddr_t
) param
;
2451 iwr
.u
.data
.length
= len
;
2453 if (ioctl(drv
->ioctl_sock
, PRISM2_IOCTL_HOSTAPD
, &iwr
) < 0) {
2454 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
2462 static struct wpa_hw_modes
*
2463 wpa_driver_wext_get_hw_feature_data(void *priv
, u16
*num_modes
, u16
*flags
)
2465 struct wpa_driver_wext_data
*drv
= priv
;
2466 struct prism2_hostapd_param
*param
;
2468 struct wpa_hw_modes
*modes
= NULL
;
2471 param
= os_zalloc(PRISM2_HOSTAPD_MAX_BUF_SIZE
);
2474 param
->cmd
= PRISM2_HOSTAPD_GET_HW_FEATURES
;
2476 if (hostapd_ioctl(drv
, param
, PRISM2_HOSTAPD_MAX_BUF_SIZE
) < 0) {
2477 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
2481 *num_modes
= param
->u
.hw_features
.num_modes
;
2482 *flags
= param
->u
.hw_features
.flags
;
2484 pos
= param
->u
.hw_features
.data
;
2485 end
= pos
+ PRISM2_HOSTAPD_MAX_BUF_SIZE
-
2486 (param
->u
.hw_features
.data
- (u8
*) param
);
2488 modes
= os_zalloc(*num_modes
* sizeof(struct wpa_hw_modes
));
2492 for (i
= 0; i
< *num_modes
; i
++) {
2493 struct hostapd_ioctl_hw_modes_hdr
*hdr
;
2494 struct wpa_hw_modes
*feature
;
2497 hdr
= (struct hostapd_ioctl_hw_modes_hdr
*) pos
;
2498 pos
= (u8
*) (hdr
+ 1);
2499 clen
= hdr
->num_channels
* sizeof(struct wpa_channel_data
);
2500 rlen
= hdr
->num_rates
* sizeof(struct wpa_rate_data
);
2502 feature
= &modes
[i
];
2503 switch (hdr
->mode
) {
2504 case MODE_IEEE80211A
:
2505 feature
->mode
= WPA_MODE_IEEE80211A
;
2507 case MODE_IEEE80211B
:
2508 feature
->mode
= WPA_MODE_IEEE80211B
;
2510 case MODE_IEEE80211G
:
2511 feature
->mode
= WPA_MODE_IEEE80211G
;
2513 case MODE_ATHEROS_TURBO
:
2514 case MODE_ATHEROS_TURBOG
:
2515 wpa_printf(MSG_ERROR
, "Skip unsupported hw_mode=%d in "
2516 "get_hw_features data", hdr
->mode
);
2520 wpa_printf(MSG_ERROR
, "Unknown hw_mode=%d in "
2521 "get_hw_features data", hdr
->mode
);
2522 wpa_supplicant_sta_free_hw_features(modes
, *num_modes
);
2526 feature
->num_channels
= hdr
->num_channels
;
2527 feature
->num_rates
= hdr
->num_rates
;
2529 feature
->channels
= os_malloc(clen
);
2530 feature
->rates
= os_malloc(rlen
);
2531 if (!feature
->channels
|| !feature
->rates
||
2532 pos
+ clen
+ rlen
> end
) {
2533 wpa_supplicant_sta_free_hw_features(modes
, *num_modes
);
2538 os_memcpy(feature
->channels
, pos
, clen
);
2540 os_memcpy(feature
->rates
, pos
, rlen
);
2550 int wpa_driver_wext_set_channel(void *priv
, wpa_hw_mode phymode
, int chan
,
2553 return wpa_driver_wext_set_freq(priv
, freq
);
2557 static void wpa_driver_wext_mlme_read(int sock
, void *eloop_ctx
,
2560 struct wpa_driver_wext_data
*drv
= eloop_ctx
;
2562 unsigned char buf
[3000];
2563 struct ieee80211_frame_info
*fi
;
2564 struct ieee80211_rx_status rx_status
;
2566 len
= recv(sock
, buf
, sizeof(buf
), 0);
2568 perror("recv[MLME]");
2572 if (len
< (int) sizeof(struct ieee80211_frame_info
)) {
2573 wpa_printf(MSG_DEBUG
, "WEXT: Too short MLME frame (len=%d)",
2578 fi
= (struct ieee80211_frame_info
*) buf
;
2579 if (ntohl(fi
->version
) != IEEE80211_FI_VERSION
) {
2580 wpa_printf(MSG_DEBUG
, "WEXT: Invalid MLME frame info version "
2581 "0x%x", ntohl(fi
->version
));
2585 os_memset(&rx_status
, 0, sizeof(rx_status
));
2586 rx_status
.ssi
= ntohl(fi
->ssi_signal
);
2587 rx_status
.channel
= ntohl(fi
->channel
);
2589 wpa_supplicant_sta_rx(drv
->ctx
,
2590 buf
+ sizeof(struct ieee80211_frame_info
),
2591 len
- sizeof(struct ieee80211_frame_info
),
2596 static int wpa_driver_wext_open_mlme(struct wpa_driver_wext_data
*drv
)
2598 int flags
, ifindex
, s
;
2599 struct sockaddr_ll addr
;
2602 if (wpa_driver_prism2_param_set(drv
, PRISM2_PARAM_USER_SPACE_MLME
, 1) <
2604 wpa_printf(MSG_ERROR
, "WEXT: Failed to configure driver to "
2605 "use user space MLME");
2609 if (wpa_driver_prism2_param_set(drv
, PRISM2_PARAM_MGMT_IF
, 1) < 0) {
2610 wpa_printf(MSG_ERROR
, "WEXT: Failed to add management "
2611 "interface for user space MLME");
2615 ifindex
= wpa_driver_prism2_param_get(drv
, PRISM2_PARAM_MGMT_IF
);
2617 wpa_printf(MSG_ERROR
, "WEXT: MLME management device not "
2622 os_memset(&ifr
, 0, sizeof(ifr
));
2623 ifr
.ifr_ifindex
= ifindex
;
2624 if (ioctl(drv
->ioctl_sock
, SIOCGIFNAME
, &ifr
) != 0) {
2625 perror("ioctl(SIOCGIFNAME)");
2628 os_strlcpy(drv
->mlmedev
, ifr
.ifr_name
, sizeof(drv
->mlmedev
));
2629 wpa_printf(MSG_DEBUG
, "WEXT: MLME management device '%s'",
2632 if (wpa_driver_wext_get_ifflags_ifname(drv
, drv
->mlmedev
, &flags
) != 0
2633 || wpa_driver_wext_set_ifflags_ifname(drv
, drv
->mlmedev
,
2634 flags
| IFF_UP
) != 0) {
2635 wpa_printf(MSG_ERROR
, "WEXT: Could not set interface "
2636 "'%s' UP", drv
->mlmedev
);
2640 s
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
2642 perror("socket[PF_PACKET,SOCK_RAW]");
2646 os_memset(&addr
, 0, sizeof(addr
));
2647 addr
.sll_family
= AF_PACKET
;
2648 addr
.sll_ifindex
= ifindex
;
2650 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2651 perror("bind(MLME)");
2655 if (eloop_register_read_sock(s
, wpa_driver_wext_mlme_read
, drv
, NULL
))
2657 wpa_printf(MSG_ERROR
, "WEXT: Could not register MLME read "
2667 static int wpa_driver_wext_send_mlme(void *priv
, const u8
*data
,
2670 struct wpa_driver_wext_data
*drv
= priv
;
2673 ret
= send(drv
->mlme_sock
, data
, data_len
, 0);
2675 perror("send[MLME]");
2683 static int wpa_driver_wext_mlme_add_sta(void *priv
, const u8
*addr
,
2684 const u8
*supp_rates
,
2685 size_t supp_rates_len
)
2687 struct wpa_driver_wext_data
*drv
= priv
;
2688 struct prism2_hostapd_param param
;
2691 os_memset(¶m
, 0, sizeof(param
));
2692 param
.cmd
= PRISM2_HOSTAPD_ADD_STA
;
2693 os_memcpy(param
.sta_addr
, addr
, ETH_ALEN
);
2694 len
= supp_rates_len
;
2695 if (len
> sizeof(param
.u
.add_sta
.supp_rates
))
2696 len
= sizeof(param
.u
.add_sta
.supp_rates
);
2697 os_memcpy(param
.u
.add_sta
.supp_rates
, supp_rates
, len
);
2698 return hostapd_ioctl(drv
, ¶m
, sizeof(param
));
2702 static int wpa_driver_wext_mlme_remove_sta(void *priv
, const u8
*addr
)
2704 struct wpa_driver_wext_data
*drv
= priv
;
2705 struct prism2_hostapd_param param
;
2707 os_memset(¶m
, 0, sizeof(param
));
2708 param
.cmd
= PRISM2_HOSTAPD_REMOVE_STA
;
2709 os_memcpy(param
.sta_addr
, addr
, ETH_ALEN
);
2710 return hostapd_ioctl(drv
, ¶m
, sizeof(param
));
2713 #endif /* CONFIG_CLIENT_MLME */
2716 static int wpa_driver_wext_set_param(void *priv
, const char *param
)
2718 #ifdef CONFIG_CLIENT_MLME
2719 struct wpa_driver_wext_data
*drv
= priv
;
2724 wpa_printf(MSG_DEBUG
, "%s: param='%s'", __func__
, param
);
2726 if (os_strstr(param
, "use_mlme=1")) {
2727 wpa_printf(MSG_DEBUG
, "WEXT: Using user space MLME");
2728 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_USER_SPACE_MLME
;
2730 drv
->mlme_sock
= wpa_driver_wext_open_mlme(drv
);
2731 if (drv
->mlme_sock
< 0)
2734 #endif /* CONFIG_CLIENT_MLME */
2740 int wpa_driver_wext_get_version(struct wpa_driver_wext_data
*drv
)
2742 return drv
->we_version_compiled
;
2746 const struct wpa_driver_ops wpa_driver_wext_ops
= {
2748 .desc
= "Linux wireless extensions (generic)",
2749 .get_bssid
= wpa_driver_wext_get_bssid
,
2750 .get_ssid
= wpa_driver_wext_get_ssid
,
2751 .set_wpa
= wpa_driver_wext_set_wpa
,
2752 .set_key
= wpa_driver_wext_set_key
,
2753 .set_countermeasures
= wpa_driver_wext_set_countermeasures
,
2754 .set_drop_unencrypted
= wpa_driver_wext_set_drop_unencrypted
,
2755 .scan
= wpa_driver_wext_scan
,
2756 .get_scan_results2
= wpa_driver_wext_get_scan_results
,
2757 .deauthenticate
= wpa_driver_wext_deauthenticate
,
2758 .disassociate
= wpa_driver_wext_disassociate
,
2759 .associate
= wpa_driver_wext_associate
,
2760 .set_auth_alg
= wpa_driver_wext_set_auth_alg
,
2761 .init
= wpa_driver_wext_init
,
2762 .deinit
= wpa_driver_wext_deinit
,
2763 .set_param
= wpa_driver_wext_set_param
,
2764 .add_pmkid
= wpa_driver_wext_add_pmkid
,
2765 .remove_pmkid
= wpa_driver_wext_remove_pmkid
,
2766 .flush_pmkid
= wpa_driver_wext_flush_pmkid
,
2767 .get_capa
= wpa_driver_wext_get_capa
,
2768 .set_operstate
= wpa_driver_wext_set_operstate
,
2769 #ifdef CONFIG_CLIENT_MLME
2770 .get_hw_feature_data
= wpa_driver_wext_get_hw_feature_data
,
2771 .set_channel
= wpa_driver_wext_set_channel
,
2772 .set_ssid
= wpa_driver_wext_set_ssid
,
2773 .set_bssid
= wpa_driver_wext_set_bssid
,
2774 .send_mlme
= wpa_driver_wext_send_mlme
,
2775 .mlme_add_sta
= wpa_driver_wext_mlme_add_sta
,
2776 .mlme_remove_sta
= wpa_driver_wext_mlme_remove_sta
,
2777 #endif /* CONFIG_CLIENT_MLME */