2 * WPA Supplicant - driver interaction with BSD net80211 layer
3 * Copyright (c) 2004, Sam Leffler <sam@errno.com>
4 * Copyright (c) 2004, 2Wire, Inc
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
17 #include <sys/ioctl.h>
22 #include "common/ieee802_11_defs.h"
27 #include <net/if_ether.h>
29 #include <net/ethernet.h>
31 #include <net/route.h>
34 #include <netproto/802_11/ieee80211_ioctl.h>
35 #include <netproto/802_11/ieee80211_dragonfly.h>
36 #else /* __DragonFly__ */
37 #include <net80211/ieee80211.h>
38 #include <net80211/ieee80211_crypto.h>
39 #include <net80211/ieee80211_ioctl.h>
40 #endif /* __DragonFly__ */
42 #include <net80211/ieee80211_freebsd.h>
45 #include <net80211/ieee80211_netbsd.h>
48 /* Generic functions for hostapd and wpa_supplicant */
51 bsd_set80211var(int s
, const char *ifname
, int op
, const void *arg
, int arg_len
)
53 struct ieee80211req ireq
;
55 os_memset(&ireq
, 0, sizeof(ireq
));
56 os_strlcpy(ireq
.i_name
, ifname
, sizeof(ireq
.i_name
));
59 ireq
.i_data
= (void *) arg
;
61 if (ioctl(s
, SIOCS80211
, &ireq
) < 0) {
62 fprintf(stderr
, "ioctl[SIOCS80211, op %u, len %u]: %s\n",
63 op
, arg_len
, strerror(errno
));
70 bsd_get80211var(int s
, const char *ifname
, int op
, void *arg
, int arg_len
)
72 struct ieee80211req ireq
;
74 os_memset(&ireq
, 0, sizeof(ireq
));
75 os_strlcpy(ireq
.i_name
, ifname
, sizeof(ireq
.i_name
));
80 if (ioctl(s
, SIOCG80211
, &ireq
) < 0) {
81 fprintf(stderr
, "ioctl[SIOCG80211, op %u, len %u]: %s\n",
82 op
, arg_len
, strerror(errno
));
89 bsd_set80211param(int s
, const char *ifname
, int op
, int arg
)
91 struct ieee80211req ireq
;
93 os_memset(&ireq
, 0, sizeof(ireq
));
94 os_strlcpy(ireq
.i_name
, ifname
, sizeof(ireq
.i_name
));
98 if (ioctl(s
, SIOCS80211
, &ireq
) < 0) {
99 fprintf(stderr
, "ioctl[SIOCS80211, op %u, arg 0x%x]: %s\n",
100 op
, arg
, strerror(errno
));
107 bsd_get_ssid(int s
, const char *ifname
, u8
*ssid
)
109 #ifdef SIOCG80211NWID
110 struct ieee80211_nwid nwid
;
113 os_memset(&ifr
, 0, sizeof(ifr
));
114 os_strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
115 ifr
.ifr_data
= (void *)&nwid
;
116 if (ioctl(s
, SIOCG80211NWID
, &ifr
) < 0 ||
117 nwid
.i_len
> IEEE80211_NWID_LEN
)
119 os_memcpy(ssid
, nwid
.i_nwid
, nwid
.i_len
);
122 return bsd_get80211var(s
, ifname
, IEEE80211_IOC_SSID
,
123 ssid
, IEEE80211_NWID_LEN
);
128 bsd_set_ssid(int s
, const char *ifname
, const u8
*ssid
, size_t ssid_len
)
130 #ifdef SIOCS80211NWID
131 struct ieee80211_nwid nwid
;
134 os_memcpy(nwid
.i_nwid
, ssid
, ssid_len
);
135 nwid
.i_len
= ssid_len
;
136 os_memset(&ifr
, 0, sizeof(ifr
));
137 os_strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
138 ifr
.ifr_data
= (void *)&nwid
;
139 return ioctl(s
, SIOCS80211NWID
, &ifr
);
141 return bsd_set80211var(s
, ifname
, IEEE80211_IOC_SSID
, ssid
, ssid_len
);
149 * Avoid conflicts with hostapd definitions by undefining couple of defines
150 * from net80211 header files.
156 #include "l2_packet/l2_packet.h"
158 struct bsd_driver_data
{
159 struct hostapd_data
*hapd
; /* back pointer */
161 char iface
[IFNAMSIZ
+ 1];
162 struct l2_packet_data
*sock_xmit
; /* raw packet xmit socket */
163 int ioctl_sock
; /* socket for ioctl() use */
164 int wext_sock
; /* socket for wireless events */
167 static int bsd_sta_deauth(void *priv
, const u8
*own_addr
, const u8
*addr
,
171 set80211var(struct bsd_driver_data
*drv
, int op
, const void *arg
, int arg_len
)
173 return bsd_set80211var(drv
->ioctl_sock
, drv
->iface
, op
, arg
, arg_len
);
177 get80211var(struct bsd_driver_data
*drv
, int op
, void *arg
, int arg_len
)
179 return bsd_get80211var(drv
->ioctl_sock
, drv
->iface
, op
, arg
, arg_len
);
183 set80211param(struct bsd_driver_data
*drv
, int op
, int arg
)
185 return bsd_set80211param(drv
->ioctl_sock
, drv
->iface
, op
, arg
);
189 ether_sprintf(const u8
*addr
)
191 static char buf
[sizeof(MACSTR
)];
194 snprintf(buf
, sizeof(buf
), MACSTR
, MAC2STR(addr
));
196 snprintf(buf
, sizeof(buf
), MACSTR
, 0,0,0,0,0,0);
201 * Configure WPA parameters.
204 bsd_configure_wpa(struct bsd_driver_data
*drv
, struct wpa_bss_params
*params
)
206 static const char *ciphernames
[] =
207 { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" };
210 switch (params
->wpa_group
) {
211 case WPA_CIPHER_CCMP
:
212 v
= IEEE80211_CIPHER_AES_CCM
;
214 case WPA_CIPHER_TKIP
:
215 v
= IEEE80211_CIPHER_TKIP
;
217 case WPA_CIPHER_WEP104
:
218 v
= IEEE80211_CIPHER_WEP
;
220 case WPA_CIPHER_WEP40
:
221 v
= IEEE80211_CIPHER_WEP
;
223 case WPA_CIPHER_NONE
:
224 v
= IEEE80211_CIPHER_NONE
;
227 printf("Unknown group key cipher %u\n",
231 wpa_printf(MSG_DEBUG
, "%s: group key cipher=%s (%u)",
232 __func__
, ciphernames
[v
], v
);
233 if (set80211param(drv
, IEEE80211_IOC_MCASTCIPHER
, v
)) {
234 printf("Unable to set group key cipher to %u (%s)\n",
238 if (v
== IEEE80211_CIPHER_WEP
) {
239 /* key length is done only for specific ciphers */
240 v
= (params
->wpa_group
== WPA_CIPHER_WEP104
? 13 : 5);
241 if (set80211param(drv
, IEEE80211_IOC_MCASTKEYLEN
, v
)) {
242 printf("Unable to set group key length to %u\n", v
);
248 if (params
->wpa_pairwise
& WPA_CIPHER_CCMP
)
249 v
|= 1<<IEEE80211_CIPHER_AES_CCM
;
250 if (params
->wpa_pairwise
& WPA_CIPHER_TKIP
)
251 v
|= 1<<IEEE80211_CIPHER_TKIP
;
252 if (params
->wpa_pairwise
& WPA_CIPHER_NONE
)
253 v
|= 1<<IEEE80211_CIPHER_NONE
;
254 wpa_printf(MSG_DEBUG
, "%s: pairwise key ciphers=0x%x", __func__
, v
);
255 if (set80211param(drv
, IEEE80211_IOC_UCASTCIPHERS
, v
)) {
256 printf("Unable to set pairwise key ciphers to 0x%x\n", v
);
260 wpa_printf(MSG_DEBUG
, "%s: key management algorithms=0x%x",
261 __func__
, params
->wpa_key_mgmt
);
262 if (set80211param(drv
, IEEE80211_IOC_KEYMGTALGS
, params
->wpa_key_mgmt
))
264 printf("Unable to set key management algorithms to 0x%x\n",
265 params
->wpa_key_mgmt
);
270 if (params
->rsn_preauth
)
272 wpa_printf(MSG_DEBUG
, "%s: rsn capabilities=0x%x",
273 __func__
, params
->rsn_preauth
);
274 if (set80211param(drv
, IEEE80211_IOC_RSNCAPS
, v
)) {
275 printf("Unable to set RSN capabilities to 0x%x\n", v
);
279 wpa_printf(MSG_DEBUG
, "%s: enable WPA= 0x%x", __func__
, params
->wpa
);
280 if (set80211param(drv
, IEEE80211_IOC_WPA
, params
->wpa
)) {
281 printf("Unable to set WPA to %u\n", params
->wpa
);
289 bsd_set_iface_flags(void *priv
, int dev_up
)
291 struct bsd_driver_data
*drv
= priv
;
294 wpa_printf(MSG_DEBUG
, "%s: dev_up=%d", __func__
, dev_up
);
296 if (drv
->ioctl_sock
< 0)
299 memset(&ifr
, 0, sizeof(ifr
));
300 os_strlcpy(ifr
.ifr_name
, drv
->iface
, sizeof(ifr
.ifr_name
));
302 if (ioctl(drv
->ioctl_sock
, SIOCGIFFLAGS
, &ifr
) != 0) {
303 perror("ioctl[SIOCGIFFLAGS]");
308 ifr
.ifr_flags
|= IFF_UP
;
310 ifr
.ifr_flags
&= ~IFF_UP
;
312 if (ioctl(drv
->ioctl_sock
, SIOCSIFFLAGS
, &ifr
) != 0) {
313 perror("ioctl[SIOCSIFFLAGS]");
321 bsd_set_ieee8021x(void *priv
, struct wpa_bss_params
*params
)
323 struct bsd_driver_data
*drv
= priv
;
325 wpa_printf(MSG_DEBUG
, "%s: enabled=%d", __func__
, params
->enabled
);
327 if (!params
->enabled
) {
328 /* XXX restore state */
329 return set80211param(priv
, IEEE80211_IOC_AUTHMODE
,
330 IEEE80211_AUTH_AUTO
);
332 if (!params
->wpa
&& !params
->ieee802_1x
) {
333 hostapd_logger(drv
->hapd
, NULL
, HOSTAPD_MODULE_DRIVER
,
334 HOSTAPD_LEVEL_WARNING
, "No 802.1X or WPA enabled!");
337 if (params
->wpa
&& bsd_configure_wpa(drv
, params
) != 0) {
338 hostapd_logger(drv
->hapd
, NULL
, HOSTAPD_MODULE_DRIVER
,
339 HOSTAPD_LEVEL_WARNING
, "Error configuring WPA state!");
342 if (set80211param(priv
, IEEE80211_IOC_AUTHMODE
,
343 (params
->wpa
? IEEE80211_AUTH_WPA
: IEEE80211_AUTH_8021X
))) {
344 hostapd_logger(drv
->hapd
, NULL
, HOSTAPD_MODULE_DRIVER
,
345 HOSTAPD_LEVEL_WARNING
, "Error enabling WPA/802.1X!");
348 return bsd_set_iface_flags(priv
, 1);
352 bsd_set_privacy(const char *ifname
, void *priv
, int enabled
)
354 struct bsd_driver_data
*drv
= priv
;
356 wpa_printf(MSG_DEBUG
, "%s: enabled=%d", __func__
, enabled
);
358 return set80211param(drv
, IEEE80211_IOC_PRIVACY
, enabled
);
362 bsd_set_sta_authorized(void *priv
, const u8
*addr
, int authorized
)
364 struct bsd_driver_data
*drv
= priv
;
365 struct ieee80211req_mlme mlme
;
367 wpa_printf(MSG_DEBUG
, "%s: addr=%s authorized=%d",
368 __func__
, ether_sprintf(addr
), authorized
);
371 mlme
.im_op
= IEEE80211_MLME_AUTHORIZE
;
373 mlme
.im_op
= IEEE80211_MLME_UNAUTHORIZE
;
375 memcpy(mlme
.im_macaddr
, addr
, IEEE80211_ADDR_LEN
);
376 return set80211var(drv
, IEEE80211_IOC_MLME
, &mlme
, sizeof(mlme
));
380 bsd_sta_set_flags(void *priv
, const u8
*addr
, int total_flags
, int flags_or
,
383 /* For now, only support setting Authorized flag */
384 if (flags_or
& WPA_STA_AUTHORIZED
)
385 return bsd_set_sta_authorized(priv
, addr
, 1);
386 if (!(flags_and
& WPA_STA_AUTHORIZED
))
387 return bsd_set_sta_authorized(priv
, addr
, 0);
392 bsd_del_key(void *priv
, const u8
*addr
, int key_idx
)
394 struct bsd_driver_data
*drv
= priv
;
395 struct ieee80211req_del_key wk
;
397 wpa_printf(MSG_DEBUG
, "%s: addr=%s key_idx=%d",
398 __func__
, ether_sprintf(addr
), key_idx
);
400 memset(&wk
, 0, sizeof(wk
));
402 memcpy(wk
.idk_macaddr
, addr
, IEEE80211_ADDR_LEN
);
403 wk
.idk_keyix
= (u_int8_t
) IEEE80211_KEYIX_NONE
; /* XXX */
405 wk
.idk_keyix
= key_idx
;
408 return set80211var(drv
, IEEE80211_IOC_DELKEY
, &wk
, sizeof(wk
));
412 bsd_set_key(const char *ifname
, void *priv
, enum wpa_alg alg
,
413 const u8
*addr
, int key_idx
, int set_tx
, const u8
*seq
,
414 size_t seq_len
, const u8
*key
, size_t key_len
)
416 struct bsd_driver_data
*drv
= priv
;
417 struct ieee80211req_key wk
;
420 if (alg
== WPA_ALG_NONE
)
421 return bsd_del_key(drv
, addr
, key_idx
);
423 wpa_printf(MSG_DEBUG
, "%s: alg=%d addr=%s key_idx=%d",
424 __func__
, alg
, ether_sprintf(addr
), key_idx
);
426 if (alg
== WPA_ALG_WEP
)
427 cipher
= IEEE80211_CIPHER_WEP
;
428 else if (alg
== WPA_ALG_TKIP
)
429 cipher
= IEEE80211_CIPHER_TKIP
;
430 else if (alg
== WPA_ALG_CCMP
)
431 cipher
= IEEE80211_CIPHER_AES_CCM
;
433 printf("%s: unknown/unsupported algorithm %d\n",
438 if (key_len
> sizeof(wk
.ik_keydata
)) {
439 printf("%s: key length %d too big\n", __func__
, (int) key_len
);
443 memset(&wk
, 0, sizeof(wk
));
445 wk
.ik_flags
= IEEE80211_KEY_RECV
| IEEE80211_KEY_XMIT
;
447 memset(wk
.ik_macaddr
, 0xff, IEEE80211_ADDR_LEN
);
448 wk
.ik_keyix
= key_idx
;
449 wk
.ik_flags
|= IEEE80211_KEY_DEFAULT
;
451 memcpy(wk
.ik_macaddr
, addr
, IEEE80211_ADDR_LEN
);
452 wk
.ik_keyix
= IEEE80211_KEYIX_NONE
;
454 wk
.ik_keylen
= key_len
;
455 memcpy(wk
.ik_keydata
, key
, key_len
);
457 return set80211var(drv
, IEEE80211_IOC_WPAKEY
, &wk
, sizeof(wk
));
462 bsd_get_seqnum(const char *ifname
, void *priv
, const u8
*addr
, int idx
,
465 struct bsd_driver_data
*drv
= priv
;
466 struct ieee80211req_key wk
;
468 wpa_printf(MSG_DEBUG
, "%s: addr=%s idx=%d",
469 __func__
, ether_sprintf(addr
), idx
);
471 memset(&wk
, 0, sizeof(wk
));
473 memset(wk
.ik_macaddr
, 0xff, IEEE80211_ADDR_LEN
);
475 memcpy(wk
.ik_macaddr
, addr
, IEEE80211_ADDR_LEN
);
478 if (get80211var(drv
, IEEE80211_IOC_WPAKEY
, &wk
, sizeof(wk
)) < 0) {
479 printf("Failed to get encryption.\n");
483 #ifdef WORDS_BIGENDIAN
486 * wk.ik_keytsc is in host byte order (big endian), need to
487 * swap it to match with the byte order used in WPA.
490 u8 tmp
[WPA_KEY_RSC_LEN
];
491 memcpy(tmp
, &wk
.ik_keytsc
, sizeof(wk
.ik_keytsc
));
492 for (i
= 0; i
< WPA_KEY_RSC_LEN
; i
++) {
493 seq
[i
] = tmp
[WPA_KEY_RSC_LEN
- i
- 1];
496 #else /* WORDS_BIGENDIAN */
497 memcpy(seq
, &wk
.ik_keytsc
, sizeof(wk
.ik_keytsc
));
498 #endif /* WORDS_BIGENDIAN */
504 bsd_flush(void *priv
)
506 u8 allsta
[IEEE80211_ADDR_LEN
];
508 memset(allsta
, 0xff, IEEE80211_ADDR_LEN
);
509 return bsd_sta_deauth(priv
, NULL
, allsta
, IEEE80211_REASON_AUTH_LEAVE
);
514 bsd_read_sta_driver_data(void *priv
, struct hostap_sta_driver_data
*data
,
517 struct bsd_driver_data
*drv
= priv
;
518 struct ieee80211req_sta_stats stats
;
520 memcpy(stats
.is_u
.macaddr
, addr
, IEEE80211_ADDR_LEN
);
521 if (get80211var(drv
, IEEE80211_IOC_STA_STATS
, &stats
, sizeof(stats
)) > 0) {
522 /* XXX? do packets counts include non-data frames? */
523 data
->rx_packets
= stats
.is_stats
.ns_rx_data
;
524 data
->rx_bytes
= stats
.is_stats
.ns_rx_bytes
;
525 data
->tx_packets
= stats
.is_stats
.ns_tx_data
;
526 data
->tx_bytes
= stats
.is_stats
.ns_tx_bytes
;
532 bsd_set_opt_ie(const char *ifname
, void *priv
, const u8
*ie
, size_t ie_len
)
535 * Do nothing; we setup parameters at startup that define the
536 * contents of the beacon information element.
542 bsd_sta_deauth(void *priv
, const u8
*own_addr
, const u8
*addr
, int reason_code
)
544 struct bsd_driver_data
*drv
= priv
;
545 struct ieee80211req_mlme mlme
;
547 wpa_printf(MSG_DEBUG
, "%s: addr=%s reason_code=%d",
548 __func__
, ether_sprintf(addr
), reason_code
);
550 mlme
.im_op
= IEEE80211_MLME_DEAUTH
;
551 mlme
.im_reason
= reason_code
;
552 memcpy(mlme
.im_macaddr
, addr
, IEEE80211_ADDR_LEN
);
553 return set80211var(drv
, IEEE80211_IOC_MLME
, &mlme
, sizeof(mlme
));
557 bsd_sta_disassoc(void *priv
, const u8
*own_addr
, const u8
*addr
,
560 struct bsd_driver_data
*drv
= priv
;
561 struct ieee80211req_mlme mlme
;
563 wpa_printf(MSG_DEBUG
, "%s: addr=%s reason_code=%d",
564 __func__
, ether_sprintf(addr
), reason_code
);
566 mlme
.im_op
= IEEE80211_MLME_DISASSOC
;
567 mlme
.im_reason
= reason_code
;
568 memcpy(mlme
.im_macaddr
, addr
, IEEE80211_ADDR_LEN
);
569 return set80211var(drv
, IEEE80211_IOC_MLME
, &mlme
, sizeof(mlme
));
573 bsd_new_sta(struct bsd_driver_data
*drv
, u8 addr
[IEEE80211_ADDR_LEN
])
575 struct hostapd_data
*hapd
= drv
->hapd
;
576 struct ieee80211req_wpaie ie
;
581 * Fetch and validate any negotiated WPA/RSN parameters.
583 memset(&ie
, 0, sizeof(ie
));
584 memcpy(ie
.wpa_macaddr
, addr
, IEEE80211_ADDR_LEN
);
585 if (get80211var(drv
, IEEE80211_IOC_WPAIE
, &ie
, sizeof(ie
)) < 0) {
586 printf("Failed to get WPA/RSN information element.\n");
590 ielen
= ie
.wpa_ie
[1];
597 drv_event_assoc(hapd
, addr
, iebuf
, ielen
);
601 bsd_wireless_event_receive(int sock
, void *ctx
, void *sock_ctx
)
603 struct bsd_driver_data
*drv
= ctx
;
605 struct if_announcemsghdr
*ifan
;
606 struct rt_msghdr
*rtm
;
607 struct ieee80211_michael_event
*mic
;
608 struct ieee80211_join_event
*join
;
609 struct ieee80211_leave_event
*leave
;
611 union wpa_event_data data
;
613 n
= read(sock
, buf
, sizeof(buf
));
615 if (errno
!= EINTR
&& errno
!= EAGAIN
)
616 perror("read(PF_ROUTE)");
620 rtm
= (struct rt_msghdr
*) buf
;
621 if (rtm
->rtm_version
!= RTM_VERSION
) {
622 wpa_printf(MSG_DEBUG
, "Routing message version %d not "
623 "understood\n", rtm
->rtm_version
);
626 ifan
= (struct if_announcemsghdr
*) rtm
;
627 switch (rtm
->rtm_type
) {
629 switch (ifan
->ifan_what
) {
630 case RTM_IEEE80211_ASSOC
:
631 case RTM_IEEE80211_REASSOC
:
632 case RTM_IEEE80211_DISASSOC
:
633 case RTM_IEEE80211_SCAN
:
635 case RTM_IEEE80211_LEAVE
:
636 leave
= (struct ieee80211_leave_event
*) &ifan
[1];
637 drv_event_notif_disassoc(drv
->hapd
, leave
->iev_addr
);
639 case RTM_IEEE80211_JOIN
:
640 #ifdef RTM_IEEE80211_REJOIN
641 case RTM_IEEE80211_REJOIN
:
643 join
= (struct ieee80211_join_event
*) &ifan
[1];
644 bsd_new_sta(drv
, join
->iev_addr
);
646 case RTM_IEEE80211_REPLAY
:
649 case RTM_IEEE80211_MICHAEL
:
650 mic
= (struct ieee80211_michael_event
*) &ifan
[1];
651 wpa_printf(MSG_DEBUG
,
652 "Michael MIC failure wireless event: "
653 "keyix=%u src_addr=" MACSTR
, mic
->iev_keyix
,
654 MAC2STR(mic
->iev_src
));
655 os_memset(&data
, 0, sizeof(data
));
656 data
.michael_mic_failure
.unicast
= 1;
657 data
.michael_mic_failure
.src
= mic
->iev_src
;
658 wpa_supplicant_event(drv
->hapd
,
659 EVENT_MICHAEL_MIC_FAILURE
, &data
);
667 bsd_wireless_event_init(struct bsd_driver_data
*drv
)
673 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
675 perror("socket(PF_ROUTE,SOCK_RAW)");
678 eloop_register_read_sock(s
, bsd_wireless_event_receive
, drv
, NULL
);
685 bsd_wireless_event_deinit(struct bsd_driver_data
*drv
)
687 if (drv
->wext_sock
< 0)
689 eloop_unregister_read_sock(drv
->wext_sock
);
690 close(drv
->wext_sock
);
695 bsd_send_eapol(void *priv
, const u8
*addr
, const u8
*data
, size_t data_len
,
696 int encrypt
, const u8
*own_addr
)
698 struct bsd_driver_data
*drv
= priv
;
699 unsigned char buf
[3000];
700 unsigned char *bp
= buf
;
701 struct l2_ethhdr
*eth
;
706 * Prepend the Etherent header. If the caller left us
707 * space at the front we could just insert it but since
708 * we don't know we copy to a local buffer. Given the frequency
709 * and size of frames this probably doesn't matter.
711 len
= data_len
+ sizeof(struct l2_ethhdr
);
712 if (len
> sizeof(buf
)) {
715 printf("EAPOL frame discarded, cannot malloc temp "
716 "buffer of size %u!\n", (unsigned int) len
);
720 eth
= (struct l2_ethhdr
*) bp
;
721 memcpy(eth
->h_dest
, addr
, ETH_ALEN
);
722 memcpy(eth
->h_source
, own_addr
, ETH_ALEN
);
723 eth
->h_proto
= htons(ETH_P_EAPOL
);
724 memcpy(eth
+1, data
, data_len
);
726 wpa_hexdump(MSG_MSGDUMP
, "TX EAPOL", bp
, len
);
728 status
= l2_packet_send(drv
->sock_xmit
, addr
, ETH_P_EAPOL
, bp
, len
);
736 handle_read(void *ctx
, const u8
*src_addr
, const u8
*buf
, size_t len
)
738 struct bsd_driver_data
*drv
= ctx
;
739 union wpa_event_data event
;
740 os_memset(&event
, 0, sizeof(event
));
741 event
.eapol_rx
.src
= src_addr
;
742 event
.eapol_rx
.data
= buf
+ sizeof(struct l2_ethhdr
);
743 event
.eapol_rx
.data_len
= len
- sizeof(struct l2_ethhdr
);
744 wpa_supplicant_event(drv
->hapd
, EVENT_EAPOL_RX
, &event
);
748 hostapd_bsd_get_ssid(const char *ifname
, void *priv
, u8
*buf
, int len
)
750 struct bsd_driver_data
*drv
= priv
;
753 ssid_len
= bsd_get_ssid(drv
->ioctl_sock
, drv
->iface
, buf
);
754 wpa_printf(MSG_DEBUG
, "%s: ssid=\"%.*s\"", __func__
, ssid_len
, buf
);
760 hostapd_bsd_set_ssid(const char *ifname
, void *priv
, const u8
*buf
, int len
)
762 struct bsd_driver_data
*drv
= priv
;
764 wpa_printf(MSG_DEBUG
, "%s: ssid=\"%.*s\"", __func__
, len
, buf
);
766 return bsd_set_ssid(drv
->ioctl_sock
, drv
->iface
, buf
, len
);
770 bsd_init(struct hostapd_data
*hapd
, struct wpa_init_params
*params
)
772 struct bsd_driver_data
*drv
;
774 drv
= os_zalloc(sizeof(struct bsd_driver_data
));
776 printf("Could not allocate memory for bsd driver data\n");
781 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
782 if (drv
->ioctl_sock
< 0) {
783 perror("socket[PF_INET,SOCK_DGRAM]");
786 memcpy(drv
->iface
, params
->ifname
, sizeof(drv
->iface
));
788 drv
->sock_xmit
= l2_packet_init(drv
->iface
, NULL
, ETH_P_EAPOL
,
789 handle_read
, drv
, 1);
790 if (drv
->sock_xmit
== NULL
)
792 if (l2_packet_get_own_addr(drv
->sock_xmit
, params
->own_addr
))
795 bsd_set_iface_flags(drv
, 0); /* mark down during setup */
796 if (bsd_wireless_event_init(drv
))
801 if (drv
->sock_xmit
!= NULL
)
802 l2_packet_deinit(drv
->sock_xmit
);
803 if (drv
->ioctl_sock
>= 0)
804 close(drv
->ioctl_sock
);
812 bsd_deinit(void *priv
)
814 struct bsd_driver_data
*drv
= priv
;
816 bsd_wireless_event_deinit(drv
);
817 (void) bsd_set_iface_flags(drv
, 0);
818 if (drv
->ioctl_sock
>= 0)
819 close(drv
->ioctl_sock
);
820 if (drv
->sock_xmit
!= NULL
)
821 l2_packet_deinit(drv
->sock_xmit
);
825 const struct wpa_driver_ops wpa_driver_bsd_ops
= {
827 .hapd_init
= bsd_init
,
828 .hapd_deinit
= bsd_deinit
,
829 .set_ieee8021x
= bsd_set_ieee8021x
,
830 .set_privacy
= bsd_set_privacy
,
831 .set_key
= bsd_set_key
,
832 .get_seqnum
= bsd_get_seqnum
,
834 .set_generic_elem
= bsd_set_opt_ie
,
835 .sta_set_flags
= bsd_sta_set_flags
,
836 .read_sta_data
= bsd_read_sta_driver_data
,
837 .hapd_send_eapol
= bsd_send_eapol
,
838 .sta_disassoc
= bsd_sta_disassoc
,
839 .sta_deauth
= bsd_sta_deauth
,
840 .hapd_set_ssid
= hostapd_bsd_set_ssid
,
841 .hapd_get_ssid
= hostapd_bsd_get_ssid
,
846 struct wpa_driver_bsd_data
{
847 int sock
; /* open socket for 802.11 ioctls */
848 int route
; /* routing socket for events */
849 char ifname
[IFNAMSIZ
+1]; /* interface name */
850 unsigned int ifindex
; /* interface index */
852 int prev_roaming
; /* roaming state to restore on deinit */
853 int prev_privacy
; /* privacy state to restore on deinit */
854 int prev_wpa
; /* wpa state to restore on deinit */
858 set80211var(struct wpa_driver_bsd_data
*drv
, int op
, const void *arg
, int arg_len
)
860 return bsd_set80211var(drv
->sock
, drv
->ifname
, op
, arg
, arg_len
);
864 get80211var(struct wpa_driver_bsd_data
*drv
, int op
, void *arg
, int arg_len
)
866 return bsd_get80211var(drv
->sock
, drv
->ifname
, op
, arg
, arg_len
);
870 set80211param(struct wpa_driver_bsd_data
*drv
, int op
, int arg
)
872 return bsd_set80211param(drv
->sock
, drv
->ifname
, op
, arg
);
876 get80211param(struct wpa_driver_bsd_data
*drv
, int op
)
878 struct ieee80211req ireq
;
880 os_memset(&ireq
, 0, sizeof(ireq
));
881 os_strlcpy(ireq
.i_name
, drv
->ifname
, sizeof(ireq
.i_name
));
884 if (ioctl(drv
->sock
, SIOCG80211
, &ireq
) < 0) {
885 fprintf(stderr
, "ioctl[SIOCG80211, op %u]: %s\n",
886 op
, strerror(errno
));
893 getifflags(struct wpa_driver_bsd_data
*drv
, int *flags
)
897 os_memset(&ifr
, 0, sizeof(ifr
));
898 os_strlcpy(ifr
.ifr_name
, drv
->ifname
, sizeof(ifr
.ifr_name
));
899 if (ioctl(drv
->sock
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
900 perror("SIOCGIFFLAGS");
903 *flags
= ifr
.ifr_flags
& 0xffff;
908 setifflags(struct wpa_driver_bsd_data
*drv
, int flags
)
912 os_memset(&ifr
, 0, sizeof(ifr
));
913 os_strlcpy(ifr
.ifr_name
, drv
->ifname
, sizeof(ifr
.ifr_name
));
914 ifr
.ifr_flags
= flags
& 0xffff;
915 if (ioctl(drv
->sock
, SIOCSIFFLAGS
, (caddr_t
)&ifr
) < 0) {
916 perror("SIOCSIFFLAGS");
923 wpa_driver_bsd_get_bssid(void *priv
, u8
*bssid
)
925 struct wpa_driver_bsd_data
*drv
= priv
;
926 #ifdef SIOCG80211BSSID
927 struct ieee80211_bssid bs
;
929 os_strncpy(bs
.i_name
, drv
->ifname
, sizeof(bs
.i_name
));
930 if (ioctl(drv
->sock
, SIOCG80211BSSID
, &bs
) < 0)
932 os_memcpy(bssid
, bs
.i_bssid
, sizeof(bs
.i_bssid
));
935 return get80211var(drv
, IEEE80211_IOC_BSSID
,
936 bssid
, IEEE80211_ADDR_LEN
) < 0 ? -1 : 0;
942 wpa_driver_bsd_set_bssid(void *priv
, const char *bssid
)
944 struct wpa_driver_bsd_data
*drv
= priv
;
946 return set80211var(drv
, IEEE80211_IOC_BSSID
,
947 bssid
, IEEE80211_ADDR_LEN
);
952 wpa_driver_bsd_get_ssid(void *priv
, u8
*ssid
)
954 struct wpa_driver_bsd_data
*drv
= priv
;
956 return bsd_get_ssid(drv
->sock
, drv
->ifname
, ssid
);
960 wpa_driver_bsd_set_ssid(void *priv
, const u8
*ssid
,
963 struct wpa_driver_bsd_data
*drv
= priv
;
965 return bsd_set_ssid(drv
->sock
, drv
->ifname
, ssid
, ssid_len
);
969 wpa_driver_bsd_set_wpa_ie(struct wpa_driver_bsd_data
*drv
,
970 const u8
*wpa_ie
, size_t wpa_ie_len
)
972 return set80211var(drv
, IEEE80211_IOC_OPTIE
, wpa_ie
, wpa_ie_len
);
976 wpa_driver_bsd_set_wpa_internal(void *priv
, int wpa
, int privacy
)
978 struct wpa_driver_bsd_data
*drv
= priv
;
981 wpa_printf(MSG_DEBUG
, "%s: wpa=%d privacy=%d",
982 __FUNCTION__
, wpa
, privacy
);
984 if (!wpa
&& wpa_driver_bsd_set_wpa_ie(drv
, NULL
, 0) < 0)
986 if (set80211param(drv
, IEEE80211_IOC_PRIVACY
, privacy
) < 0)
988 if (set80211param(drv
, IEEE80211_IOC_WPA
, wpa
) < 0)
995 wpa_driver_bsd_set_wpa(void *priv
, int enabled
)
997 wpa_printf(MSG_DEBUG
, "%s: enabled=%d", __FUNCTION__
, enabled
);
999 return wpa_driver_bsd_set_wpa_internal(priv
, enabled
? 3 : 0, enabled
);
1003 wpa_driver_bsd_del_key(struct wpa_driver_bsd_data
*drv
, int key_idx
,
1004 const unsigned char *addr
)
1006 struct ieee80211req_del_key wk
;
1008 os_memset(&wk
, 0, sizeof(wk
));
1010 bcmp(addr
, "\xff\xff\xff\xff\xff\xff", IEEE80211_ADDR_LEN
) != 0) {
1011 struct ether_addr ea
;
1013 os_memcpy(&ea
, addr
, IEEE80211_ADDR_LEN
);
1014 wpa_printf(MSG_DEBUG
, "%s: addr=%s keyidx=%d",
1015 __func__
, ether_ntoa(&ea
), key_idx
);
1016 os_memcpy(wk
.idk_macaddr
, addr
, IEEE80211_ADDR_LEN
);
1017 wk
.idk_keyix
= (uint8_t) IEEE80211_KEYIX_NONE
;
1019 wpa_printf(MSG_DEBUG
, "%s: keyidx=%d", __func__
, key_idx
);
1020 wk
.idk_keyix
= key_idx
;
1022 return set80211var(drv
, IEEE80211_IOC_DELKEY
, &wk
, sizeof(wk
));
1026 wpa_driver_bsd_set_key(const char *ifname
, void *priv
, enum wpa_alg alg
,
1027 const unsigned char *addr
, int key_idx
, int set_tx
,
1028 const u8
*seq
, size_t seq_len
,
1029 const u8
*key
, size_t key_len
)
1031 struct wpa_driver_bsd_data
*drv
= priv
;
1032 struct ieee80211req_key wk
;
1033 struct ether_addr ea
;
1037 if (alg
== WPA_ALG_NONE
)
1038 return wpa_driver_bsd_del_key(drv
, key_idx
, addr
);
1043 cipher
= IEEE80211_CIPHER_WEP
;
1047 cipher
= IEEE80211_CIPHER_TKIP
;
1051 cipher
= IEEE80211_CIPHER_AES_CCM
;
1054 wpa_printf(MSG_DEBUG
, "%s: unknown/unsupported algorithm %d",
1059 os_memcpy(&ea
, addr
, IEEE80211_ADDR_LEN
);
1060 wpa_printf(MSG_DEBUG
,
1061 "%s: alg=%s addr=%s key_idx=%d set_tx=%d seq_len=%zu key_len=%zu",
1062 __func__
, alg_name
, ether_ntoa(&ea
), key_idx
, set_tx
,
1065 if (seq_len
> sizeof(u_int64_t
)) {
1066 wpa_printf(MSG_DEBUG
, "%s: seq_len %zu too big",
1070 if (key_len
> sizeof(wk
.ik_keydata
)) {
1071 wpa_printf(MSG_DEBUG
, "%s: key length %zu too big",
1076 os_memset(&wk
, 0, sizeof(wk
));
1077 wk
.ik_type
= cipher
;
1078 wk
.ik_flags
= IEEE80211_KEY_RECV
;
1080 wk
.ik_flags
|= IEEE80211_KEY_XMIT
;
1081 os_memcpy(wk
.ik_macaddr
, addr
, IEEE80211_ADDR_LEN
);
1083 * Deduce whether group/global or unicast key by checking
1084 * the address (yech). Note also that we can only mark global
1085 * keys default; doing this for a unicast key is an error.
1087 if (bcmp(addr
, "\xff\xff\xff\xff\xff\xff", IEEE80211_ADDR_LEN
) == 0) {
1088 wk
.ik_flags
|= IEEE80211_KEY_GROUP
;
1089 wk
.ik_keyix
= key_idx
;
1091 wk
.ik_keyix
= (key_idx
== 0 ? IEEE80211_KEYIX_NONE
: key_idx
);
1093 if (wk
.ik_keyix
!= IEEE80211_KEYIX_NONE
&& set_tx
)
1094 wk
.ik_flags
|= IEEE80211_KEY_DEFAULT
;
1095 wk
.ik_keylen
= key_len
;
1096 os_memcpy(&wk
.ik_keyrsc
, seq
, seq_len
);
1097 os_memcpy(wk
.ik_keydata
, key
, key_len
);
1099 return set80211var(drv
, IEEE80211_IOC_WPAKEY
, &wk
, sizeof(wk
));
1103 wpa_driver_bsd_set_countermeasures(void *priv
, int enabled
)
1105 struct wpa_driver_bsd_data
*drv
= priv
;
1107 wpa_printf(MSG_DEBUG
, "%s: enabled=%d", __func__
, enabled
);
1108 return set80211param(drv
, IEEE80211_IOC_COUNTERMEASURES
, enabled
);
1113 wpa_driver_bsd_set_drop_unencrypted(void *priv
, int enabled
)
1115 struct wpa_driver_bsd_data
*drv
= priv
;
1117 wpa_printf(MSG_DEBUG
, "%s: enabled=%d", __func__
, enabled
);
1118 return set80211param(drv
, IEEE80211_IOC_DROPUNENCRYPTED
, enabled
);
1122 wpa_driver_bsd_deauthenticate(void *priv
, const u8
*addr
, int reason_code
)
1124 struct wpa_driver_bsd_data
*drv
= priv
;
1125 struct ieee80211req_mlme mlme
;
1127 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1128 os_memset(&mlme
, 0, sizeof(mlme
));
1129 mlme
.im_op
= IEEE80211_MLME_DEAUTH
;
1130 mlme
.im_reason
= reason_code
;
1131 os_memcpy(mlme
.im_macaddr
, addr
, IEEE80211_ADDR_LEN
);
1132 return set80211var(drv
, IEEE80211_IOC_MLME
, &mlme
, sizeof(mlme
));
1136 wpa_driver_bsd_disassociate(void *priv
, const u8
*addr
, int reason_code
)
1138 struct wpa_driver_bsd_data
*drv
= priv
;
1139 struct ieee80211req_mlme mlme
;
1141 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1142 os_memset(&mlme
, 0, sizeof(mlme
));
1143 mlme
.im_op
= IEEE80211_MLME_DISASSOC
;
1144 mlme
.im_reason
= reason_code
;
1145 os_memcpy(mlme
.im_macaddr
, addr
, IEEE80211_ADDR_LEN
);
1146 return set80211var(drv
, IEEE80211_IOC_MLME
, &mlme
, sizeof(mlme
));
1150 wpa_driver_bsd_set_auth_alg(void *priv
, int auth_alg
)
1152 struct wpa_driver_bsd_data
*drv
= priv
;
1155 if ((auth_alg
& AUTH_ALG_OPEN_SYSTEM
) &&
1156 (auth_alg
& AUTH_ALG_SHARED_KEY
))
1157 authmode
= IEEE80211_AUTH_AUTO
;
1158 else if (auth_alg
& AUTH_ALG_SHARED_KEY
)
1159 authmode
= IEEE80211_AUTH_SHARED
;
1161 authmode
= IEEE80211_AUTH_OPEN
;
1163 return set80211param(drv
, IEEE80211_IOC_AUTHMODE
, authmode
);
1167 wpa_driver_bsd_associate(void *priv
, struct wpa_driver_associate_params
*params
)
1169 struct wpa_driver_bsd_data
*drv
= priv
;
1170 struct ieee80211req_mlme mlme
;
1174 wpa_printf(MSG_DEBUG
,
1175 "%s: ssid '%.*s' wpa ie len %u pairwise %u group %u key mgmt %u"
1177 , (unsigned int) params
->ssid_len
, params
->ssid
1178 , (unsigned int) params
->wpa_ie_len
1179 , params
->pairwise_suite
1180 , params
->group_suite
1181 , params
->key_mgmt_suite
1184 if (wpa_driver_bsd_set_drop_unencrypted(drv
, params
->drop_unencrypted
)
1187 if (wpa_driver_bsd_set_auth_alg(drv
, params
->auth_alg
) < 0)
1189 /* XXX error handling is wrong but unclear what to do... */
1190 if (wpa_driver_bsd_set_wpa_ie(drv
, params
->wpa_ie
, params
->wpa_ie_len
) < 0)
1193 privacy
= !(params
->pairwise_suite
== CIPHER_NONE
&&
1194 params
->group_suite
== CIPHER_NONE
&&
1195 params
->key_mgmt_suite
== KEY_MGMT_NONE
&&
1196 params
->wpa_ie_len
== 0);
1197 wpa_printf(MSG_DEBUG
, "%s: set PRIVACY %u", __func__
, privacy
);
1199 if (set80211param(drv
, IEEE80211_IOC_PRIVACY
, privacy
) < 0)
1202 if (params
->wpa_ie_len
&&
1203 set80211param(drv
, IEEE80211_IOC_WPA
,
1204 params
->wpa_ie
[0] == WLAN_EID_RSN
? 2 : 1) < 0)
1207 os_memset(&mlme
, 0, sizeof(mlme
));
1208 mlme
.im_op
= IEEE80211_MLME_ASSOC
;
1209 if (params
->ssid
!= NULL
)
1210 os_memcpy(mlme
.im_ssid
, params
->ssid
, params
->ssid_len
);
1211 mlme
.im_ssid_len
= params
->ssid_len
;
1212 if (params
->bssid
!= NULL
)
1213 os_memcpy(mlme
.im_macaddr
, params
->bssid
, IEEE80211_ADDR_LEN
);
1214 if (set80211var(drv
, IEEE80211_IOC_MLME
, &mlme
, sizeof(mlme
)) < 0)
1220 wpa_driver_bsd_scan(void *priv
, struct wpa_driver_scan_params
*params
)
1222 struct wpa_driver_bsd_data
*drv
= priv
;
1224 const u8
*ssid
= params
->ssids
[0].ssid
;
1225 size_t ssid_len
= params
->ssids
[0].ssid_len
;
1227 /* NB: interface must be marked UP to do a scan */
1228 if (getifflags(drv
, &flags
) != 0 || setifflags(drv
, flags
| IFF_UP
) != 0)
1231 /* set desired ssid before scan */
1232 if (wpa_driver_bsd_set_ssid(drv
, ssid
, ssid_len
) < 0)
1235 /* NB: net80211 delivers a scan complete event so no need to poll */
1236 return set80211param(drv
, IEEE80211_IOC_SCAN_REQ
, 0);
1240 wpa_driver_bsd_event_receive(int sock
, void *ctx
, void *sock_ctx
)
1242 struct wpa_driver_bsd_data
*drv
= sock_ctx
;
1244 struct if_announcemsghdr
*ifan
;
1245 struct if_msghdr
*ifm
;
1246 struct rt_msghdr
*rtm
;
1247 union wpa_event_data event
;
1248 struct ieee80211_michael_event
*mic
;
1251 n
= read(sock
, buf
, sizeof(buf
));
1253 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1254 perror("read(PF_ROUTE)");
1258 rtm
= (struct rt_msghdr
*) buf
;
1259 if (rtm
->rtm_version
!= RTM_VERSION
) {
1260 wpa_printf(MSG_DEBUG
, "Routing message version %d not "
1261 "understood\n", rtm
->rtm_version
);
1264 os_memset(&event
, 0, sizeof(event
));
1265 switch (rtm
->rtm_type
) {
1266 case RTM_IFANNOUNCE
:
1267 ifan
= (struct if_announcemsghdr
*) rtm
;
1268 if (ifan
->ifan_index
!= drv
->ifindex
)
1270 strlcpy(event
.interface_status
.ifname
, drv
->ifname
,
1271 sizeof(event
.interface_status
.ifname
));
1272 switch (ifan
->ifan_what
) {
1273 case IFAN_DEPARTURE
:
1274 event
.interface_status
.ievent
= EVENT_INTERFACE_REMOVED
;
1278 wpa_printf(MSG_DEBUG
, "RTM_IFANNOUNCE: Interface '%s' %s",
1279 event
.interface_status
.ifname
,
1280 ifan
->ifan_what
== IFAN_DEPARTURE
?
1281 "removed" : "added");
1282 wpa_supplicant_event(ctx
, EVENT_INTERFACE_STATUS
, &event
);
1285 ifan
= (struct if_announcemsghdr
*) rtm
;
1286 if (ifan
->ifan_index
!= drv
->ifindex
)
1288 switch (ifan
->ifan_what
) {
1289 case RTM_IEEE80211_ASSOC
:
1290 case RTM_IEEE80211_REASSOC
:
1291 wpa_supplicant_event(ctx
, EVENT_ASSOC
, NULL
);
1293 case RTM_IEEE80211_DISASSOC
:
1294 wpa_supplicant_event(ctx
, EVENT_DISASSOC
, NULL
);
1296 case RTM_IEEE80211_SCAN
:
1297 wpa_supplicant_event(ctx
, EVENT_SCAN_RESULTS
, NULL
);
1299 case RTM_IEEE80211_REPLAY
:
1302 case RTM_IEEE80211_MICHAEL
:
1303 mic
= (struct ieee80211_michael_event
*) &ifan
[1];
1304 wpa_printf(MSG_DEBUG
,
1305 "Michael MIC failure wireless event: "
1306 "keyix=%u src_addr=" MACSTR
, mic
->iev_keyix
,
1307 MAC2STR(mic
->iev_src
));
1309 os_memset(&event
, 0, sizeof(event
));
1310 event
.michael_mic_failure
.unicast
=
1311 !IEEE80211_IS_MULTICAST(mic
->iev_dst
);
1312 wpa_supplicant_event(ctx
, EVENT_MICHAEL_MIC_FAILURE
,
1318 ifm
= (struct if_msghdr
*) rtm
;
1319 if (ifm
->ifm_index
!= drv
->ifindex
)
1321 if ((rtm
->rtm_flags
& RTF_UP
) == 0) {
1322 strlcpy(event
.interface_status
.ifname
, drv
->ifname
,
1323 sizeof(event
.interface_status
.ifname
));
1324 event
.interface_status
.ievent
= EVENT_INTERFACE_REMOVED
;
1325 wpa_printf(MSG_DEBUG
, "RTM_IFINFO: Interface '%s' DOWN",
1326 event
.interface_status
.ifname
);
1327 wpa_supplicant_event(ctx
, EVENT_INTERFACE_STATUS
, &event
);
1334 wpa_driver_bsd_add_scan_entry(struct wpa_scan_results
*res
,
1335 struct ieee80211req_scan_result
*sr
)
1337 struct wpa_scan_res
*result
, **tmp
;
1341 extra_len
= 2 + sr
->isr_ssid_len
;
1342 extra_len
+= 2 + sr
->isr_nrates
;
1343 extra_len
+= 3; /* ERP IE */
1344 extra_len
+= sr
->isr_ie_len
;
1346 result
= os_zalloc(sizeof(*result
) + extra_len
);
1349 os_memcpy(result
->bssid
, sr
->isr_bssid
, ETH_ALEN
);
1350 result
->freq
= sr
->isr_freq
;
1351 result
->beacon_int
= sr
->isr_intval
;
1352 result
->caps
= sr
->isr_capinfo
;
1353 result
->qual
= sr
->isr_rssi
;
1354 result
->noise
= sr
->isr_noise
;
1356 pos
= (u8
*)(result
+ 1);
1358 *pos
++ = WLAN_EID_SSID
;
1359 *pos
++ = sr
->isr_ssid_len
;
1360 os_memcpy(pos
, sr
+ 1, sr
->isr_ssid_len
);
1361 pos
+= sr
->isr_ssid_len
;
1364 * Deal all rates as supported rate.
1365 * Because net80211 doesn't report extended supported rate or not.
1367 *pos
++ = WLAN_EID_SUPP_RATES
;
1368 *pos
++ = sr
->isr_nrates
;
1369 os_memcpy(pos
, sr
->isr_rates
, sr
->isr_nrates
);
1370 pos
+= sr
->isr_nrates
;
1372 *pos
++ = WLAN_EID_ERP_INFO
;
1374 *pos
++ = sr
->isr_erp
;
1376 os_memcpy(pos
, (u8
*)(sr
+ 1) + sr
->isr_ssid_len
, sr
->isr_ie_len
);
1377 pos
+= sr
->isr_ie_len
;
1379 result
->ie_len
= pos
- (u8
*)(result
+ 1);
1381 tmp
= os_realloc(res
->res
,
1382 (res
->num
+ 1) * sizeof(struct wpa_scan_res
*));
1387 tmp
[res
->num
++] = result
;
1391 struct wpa_scan_results
*
1392 wpa_driver_bsd_get_scan_results2(void *priv
)
1394 struct wpa_driver_bsd_data
*drv
= priv
;
1395 struct ieee80211req_scan_result
*sr
;
1396 struct wpa_scan_results
*res
;
1398 uint8_t buf
[24*1024], *pos
;
1400 len
= get80211var(drv
, IEEE80211_IOC_SCAN_RESULTS
, buf
, 24*1024);
1404 res
= os_zalloc(sizeof(*res
));
1410 while (rest
>= sizeof(struct ieee80211req_scan_result
)) {
1411 sr
= (struct ieee80211req_scan_result
*)pos
;
1412 wpa_driver_bsd_add_scan_entry(res
, sr
);
1414 rest
-= sr
->isr_len
;
1417 wpa_printf(MSG_DEBUG
, "Received %d bytes of scan results (%lu BSSes)",
1418 len
, (unsigned long)res
->num
);
1424 wpa_driver_bsd_init(void *ctx
, const char *ifname
)
1426 #define GETPARAM(drv, param, v) \
1427 (((v) = get80211param(drv, param)) != -1)
1428 struct wpa_driver_bsd_data
*drv
;
1430 drv
= os_zalloc(sizeof(*drv
));
1434 * NB: We require the interface name be mappable to an index.
1435 * This implies we do not support having wpa_supplicant
1436 * wait for an interface to appear. This seems ok; that
1437 * doesn't belong here; it's really the job of devd.
1439 drv
->ifindex
= if_nametoindex(ifname
);
1440 if (drv
->ifindex
== 0) {
1441 wpa_printf(MSG_DEBUG
, "%s: interface %s does not exist",
1445 drv
->sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
1448 drv
->route
= socket(PF_ROUTE
, SOCK_RAW
, 0);
1451 eloop_register_read_sock(drv
->route
,
1452 wpa_driver_bsd_event_receive
, ctx
, drv
);
1455 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
1457 if (!GETPARAM(drv
, IEEE80211_IOC_ROAMING
, drv
->prev_roaming
)) {
1458 wpa_printf(MSG_DEBUG
, "%s: failed to get roaming state: %s",
1459 __func__
, strerror(errno
));
1462 if (!GETPARAM(drv
, IEEE80211_IOC_PRIVACY
, drv
->prev_privacy
)) {
1463 wpa_printf(MSG_DEBUG
, "%s: failed to get privacy state: %s",
1464 __func__
, strerror(errno
));
1467 if (!GETPARAM(drv
, IEEE80211_IOC_WPA
, drv
->prev_wpa
)) {
1468 wpa_printf(MSG_DEBUG
, "%s: failed to get wpa state: %s",
1469 __func__
, strerror(errno
));
1472 if (set80211param(drv
, IEEE80211_IOC_ROAMING
, IEEE80211_ROAMING_MANUAL
) < 0) {
1473 wpa_printf(MSG_DEBUG
, "%s: failed to set wpa_supplicant-based "
1474 "roaming: %s", __func__
, strerror(errno
));
1478 if (set80211param(drv
, IEEE80211_IOC_WPA
, 1+2) < 0) {
1479 wpa_printf(MSG_DEBUG
, "%s: failed to enable WPA support %s",
1480 __func__
, strerror(errno
));
1484 wpa_driver_bsd_set_wpa(drv
, 1);
1496 wpa_driver_bsd_deinit(void *priv
)
1498 struct wpa_driver_bsd_data
*drv
= priv
;
1501 wpa_driver_bsd_set_wpa(drv
, 0);
1502 eloop_unregister_read_sock(drv
->route
);
1504 /* NB: mark interface down */
1505 if (getifflags(drv
, &flags
) == 0)
1506 (void) setifflags(drv
, flags
&~ IFF_UP
);
1508 wpa_driver_bsd_set_wpa_internal(drv
, drv
->prev_wpa
, drv
->prev_privacy
);
1509 if (set80211param(drv
, IEEE80211_IOC_ROAMING
, drv
->prev_roaming
) < 0)
1510 wpa_printf(MSG_DEBUG
, "%s: failed to restore roaming state",
1513 (void) close(drv
->route
); /* ioctl socket */
1514 (void) close(drv
->sock
); /* event socket */
1519 const struct wpa_driver_ops wpa_driver_bsd_ops
= {
1521 .desc
= "BSD 802.11 support",
1522 .init
= wpa_driver_bsd_init
,
1523 .deinit
= wpa_driver_bsd_deinit
,
1524 .get_bssid
= wpa_driver_bsd_get_bssid
,
1525 .get_ssid
= wpa_driver_bsd_get_ssid
,
1526 .set_key
= wpa_driver_bsd_set_key
,
1527 .set_countermeasures
= wpa_driver_bsd_set_countermeasures
,
1528 .scan2
= wpa_driver_bsd_scan
,
1529 .get_scan_results2
= wpa_driver_bsd_get_scan_results2
,
1530 .deauthenticate
= wpa_driver_bsd_deauthenticate
,
1531 .disassociate
= wpa_driver_bsd_disassociate
,
1532 .associate
= wpa_driver_bsd_associate
,
1535 #endif /* HOSTAPD */