Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / src / drivers / driver_bsd.c
blob99de6c7ff48e4b0573db0559c89ab9846edb2a7e
1 /*
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
11 * license.
13 * See README and COPYING for more details.
16 #include "includes.h"
17 #include <sys/ioctl.h>
19 #include "common.h"
20 #include "driver.h"
21 #include "eloop.h"
22 #include "common/ieee802_11_defs.h"
24 #include <net/if.h>
25 #include <net/if_media.h>
27 #ifdef __NetBSD__
28 #include <net/if_ether.h>
29 #else
30 #include <net/ethernet.h>
31 #endif
32 #include <net/route.h>
34 #ifdef __DragonFly__
35 #include <netproto/802_11/ieee80211_ioctl.h>
36 #include <netproto/802_11/ieee80211_dragonfly.h>
37 #else /* __DragonFly__ */
38 #ifdef __GLIBC__
39 #include <netinet/ether.h>
40 #endif /* __GLIBC__ */
41 #include <net80211/ieee80211.h>
42 #include <net80211/ieee80211_ioctl.h>
43 #include <net80211/ieee80211_crypto.h>
44 #endif /* __DragonFly__ || __GLIBC__ */
45 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
46 #include <net80211/ieee80211_freebsd.h>
47 #endif
48 #if __NetBSD__
49 #include <net80211/ieee80211_netbsd.h>
50 #endif
52 #include "l2_packet/l2_packet.h"
54 struct bsd_driver_data {
55 struct hostapd_data *hapd; /* back pointer */
57 int sock; /* open socket for 802.11 ioctls */
58 struct l2_packet_data *sock_xmit;/* raw packet xmit socket */
59 int route; /* routing socket for events */
60 char ifname[IFNAMSIZ+1]; /* interface name */
61 unsigned int ifindex; /* interface index */
62 void *ctx;
63 struct wpa_driver_capa capa; /* driver capability */
64 int is_ap; /* Access point mode */
65 int prev_roaming; /* roaming state to restore on deinit */
66 int prev_privacy; /* privacy state to restore on deinit */
67 int prev_wpa; /* wpa state to restore on deinit */
70 /* Generic functions for hostapd and wpa_supplicant */
72 static int
73 bsd_set80211(void *priv, int op, int val, const void *arg, int arg_len)
75 struct bsd_driver_data *drv = priv;
76 struct ieee80211req ireq;
78 os_memset(&ireq, 0, sizeof(ireq));
79 os_strlcpy(ireq.i_name, drv->ifname, sizeof(ireq.i_name));
80 ireq.i_type = op;
81 ireq.i_val = val;
82 ireq.i_data = (void *) arg;
83 ireq.i_len = arg_len;
85 if (ioctl(drv->sock, SIOCS80211, &ireq) < 0) {
86 wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, val=%u, "
87 "arg_len=%u]: %s", op, val, arg_len,
88 strerror(errno));
89 return -1;
91 return 0;
94 static int
95 bsd_get80211(void *priv, struct ieee80211req *ireq, int op, void *arg,
96 int arg_len)
98 struct bsd_driver_data *drv = priv;
100 os_memset(ireq, 0, sizeof(*ireq));
101 os_strlcpy(ireq->i_name, drv->ifname, sizeof(ireq->i_name));
102 ireq->i_type = op;
103 ireq->i_len = arg_len;
104 ireq->i_data = arg;
106 if (ioctl(drv->sock, SIOCG80211, ireq) < 0) {
107 wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, "
108 "arg_len=%u]: %s", op, arg_len, strerror(errno));
109 return -1;
111 return 0;
114 static int
115 get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len)
117 struct ieee80211req ireq;
119 if (bsd_get80211(drv, &ireq, op, arg, arg_len) < 0)
120 return -1;
121 return ireq.i_len;
124 static int
125 set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len)
127 return bsd_set80211(drv, op, 0, arg, arg_len);
130 static int
131 set80211param(struct bsd_driver_data *drv, int op, int arg)
133 return bsd_set80211(drv, op, arg, NULL, 0);
136 static int
137 bsd_get_ssid(void *priv, u8 *ssid, int len)
139 struct bsd_driver_data *drv = priv;
140 #ifdef SIOCG80211NWID
141 struct ieee80211_nwid nwid;
142 struct ifreq ifr;
144 os_memset(&ifr, 0, sizeof(ifr));
145 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
146 ifr.ifr_data = (void *)&nwid;
147 if (ioctl(drv->sock, SIOCG80211NWID, &ifr) < 0 ||
148 nwid.i_len > IEEE80211_NWID_LEN)
149 return -1;
150 os_memcpy(ssid, nwid.i_nwid, nwid.i_len);
151 return nwid.i_len;
152 #else
153 return get80211var(drv, IEEE80211_IOC_SSID, ssid, IEEE80211_NWID_LEN);
154 #endif
157 static int
158 bsd_set_ssid(void *priv, const u8 *ssid, int ssid_len)
160 struct bsd_driver_data *drv = priv;
161 #ifdef SIOCS80211NWID
162 struct ieee80211_nwid nwid;
163 struct ifreq ifr;
165 os_memcpy(nwid.i_nwid, ssid, ssid_len);
166 nwid.i_len = ssid_len;
167 os_memset(&ifr, 0, sizeof(ifr));
168 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
169 ifr.ifr_data = (void *)&nwid;
170 return ioctl(drv->sock, SIOCS80211NWID, &ifr);
171 #else
172 return set80211var(drv, IEEE80211_IOC_SSID, ssid, ssid_len);
173 #endif
176 static int
177 bsd_get_if_media(void *priv)
179 struct bsd_driver_data *drv = priv;
180 struct ifmediareq ifmr;
182 os_memset(&ifmr, 0, sizeof(ifmr));
183 os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name));
185 if (ioctl(drv->sock, SIOCGIFMEDIA, &ifmr) < 0) {
186 wpa_printf(MSG_ERROR, "%s: SIOCGIFMEDIA %s", __func__,
187 strerror(errno));
188 return -1;
191 return ifmr.ifm_current;
194 static int
195 bsd_set_if_media(void *priv, int media)
197 struct bsd_driver_data *drv = priv;
198 struct ifreq ifr;
200 os_memset(&ifr, 0, sizeof(ifr));
201 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
202 ifr.ifr_media = media;
204 if (ioctl(drv->sock, SIOCSIFMEDIA, &ifr) < 0) {
205 wpa_printf(MSG_ERROR, "%s: SIOCSIFMEDIA %s", __func__,
206 strerror(errno));
207 return -1;
210 return 0;
213 static int
214 bsd_set_mediaopt(void *priv, uint32_t mask, uint32_t mode)
216 int media = bsd_get_if_media(priv);
218 if (media < 0)
219 return -1;
220 media &= ~mask;
221 media |= mode;
222 if (bsd_set_if_media(priv, media) < 0)
223 return -1;
224 return 0;
227 static int
228 bsd_del_key(void *priv, const u8 *addr, int key_idx)
230 struct ieee80211req_del_key wk;
232 os_memset(&wk, 0, sizeof(wk));
233 if (addr == NULL) {
234 wpa_printf(MSG_DEBUG, "%s: key_idx=%d", __func__, key_idx);
235 wk.idk_keyix = key_idx;
236 } else {
237 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__,
238 MAC2STR(addr));
239 os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
240 wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
243 return set80211var(priv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
246 static int
247 bsd_send_mlme_param(void *priv, const u8 op, const u16 reason, const u8 *addr)
249 struct ieee80211req_mlme mlme;
251 os_memset(&mlme, 0, sizeof(mlme));
252 mlme.im_op = op;
253 mlme.im_reason = reason;
254 os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
255 return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
258 static int
259 bsd_ctrl_iface(void *priv, int enable)
261 struct bsd_driver_data *drv = priv;
262 struct ifreq ifr;
264 os_memset(&ifr, 0, sizeof(ifr));
265 os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
267 if (ioctl(drv->sock, SIOCGIFFLAGS, &ifr) < 0) {
268 perror("ioctl[SIOCGIFFLAGS]");
269 return -1;
272 if (enable)
273 ifr.ifr_flags |= IFF_UP;
274 else
275 ifr.ifr_flags &= ~IFF_UP;
277 if (ioctl(drv->sock, SIOCSIFFLAGS, &ifr) < 0) {
278 perror("ioctl[SIOCSIFFLAGS]");
279 return -1;
282 return 0;
285 static int
286 bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
287 const unsigned char *addr, int key_idx, int set_tx, const u8 *seq,
288 size_t seq_len, const u8 *key, size_t key_len)
290 struct ieee80211req_key wk;
292 wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d "
293 "seq_len=%zu key_len=%zu", __func__, alg, addr, key_idx,
294 set_tx, seq_len, key_len);
296 if (alg == WPA_ALG_NONE) {
297 #ifndef HOSTAPD
298 if (addr == NULL ||
299 os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
300 IEEE80211_ADDR_LEN) == 0)
301 return bsd_del_key(priv, NULL, key_idx);
302 else
303 #endif /* HOSTAPD */
304 return bsd_del_key(priv, addr, key_idx);
307 os_memset(&wk, 0, sizeof(wk));
308 switch (alg) {
309 case WPA_ALG_WEP:
310 wk.ik_type = IEEE80211_CIPHER_WEP;
311 break;
312 case WPA_ALG_TKIP:
313 wk.ik_type = IEEE80211_CIPHER_TKIP;
314 break;
315 case WPA_ALG_CCMP:
316 wk.ik_type = IEEE80211_CIPHER_AES_CCM;
317 break;
318 default:
319 wpa_printf(MSG_ERROR, "%s: unknown alg=%d", __func__, alg);
320 return -1;
323 wk.ik_flags = IEEE80211_KEY_RECV;
324 if (set_tx)
325 wk.ik_flags |= IEEE80211_KEY_XMIT;
327 if (addr == NULL) {
328 os_memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
329 wk.ik_keyix = key_idx;
330 } else {
331 os_memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
333 * Deduce whether group/global or unicast key by checking
334 * the address (yech). Note also that we can only mark global
335 * keys default; doing this for a unicast key is an error.
337 if (os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
338 IEEE80211_ADDR_LEN) == 0) {
339 wk.ik_flags |= IEEE80211_KEY_GROUP;
340 wk.ik_keyix = key_idx;
341 } else {
342 wk.ik_keyix = key_idx == 0 ? IEEE80211_KEYIX_NONE :
343 key_idx;
346 if (wk.ik_keyix != IEEE80211_KEYIX_NONE && set_tx)
347 wk.ik_flags |= IEEE80211_KEY_DEFAULT;
348 wk.ik_keylen = key_len;
349 os_memcpy(&wk.ik_keyrsc, seq, seq_len);
350 os_memcpy(wk.ik_keydata, key, key_len);
352 return set80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk));
355 static int
356 bsd_configure_wpa(void *priv, struct wpa_bss_params *params)
358 #ifndef IEEE80211_IOC_APPIE
359 static const char *ciphernames[] =
360 { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" };
361 int v;
363 switch (params->wpa_group) {
364 case WPA_CIPHER_CCMP:
365 v = IEEE80211_CIPHER_AES_CCM;
366 break;
367 case WPA_CIPHER_TKIP:
368 v = IEEE80211_CIPHER_TKIP;
369 break;
370 case WPA_CIPHER_WEP104:
371 v = IEEE80211_CIPHER_WEP;
372 break;
373 case WPA_CIPHER_WEP40:
374 v = IEEE80211_CIPHER_WEP;
375 break;
376 case WPA_CIPHER_NONE:
377 v = IEEE80211_CIPHER_NONE;
378 break;
379 default:
380 printf("Unknown group key cipher %u\n",
381 params->wpa_group);
382 return -1;
384 wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)",
385 __func__, ciphernames[v], v);
386 if (set80211param(priv, IEEE80211_IOC_MCASTCIPHER, v)) {
387 printf("Unable to set group key cipher to %u (%s)\n",
388 v, ciphernames[v]);
389 return -1;
391 if (v == IEEE80211_CIPHER_WEP) {
392 /* key length is done only for specific ciphers */
393 v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
394 if (set80211param(priv, IEEE80211_IOC_MCASTKEYLEN, v)) {
395 printf("Unable to set group key length to %u\n", v);
396 return -1;
400 v = 0;
401 if (params->wpa_pairwise & WPA_CIPHER_CCMP)
402 v |= 1<<IEEE80211_CIPHER_AES_CCM;
403 if (params->wpa_pairwise & WPA_CIPHER_TKIP)
404 v |= 1<<IEEE80211_CIPHER_TKIP;
405 if (params->wpa_pairwise & WPA_CIPHER_NONE)
406 v |= 1<<IEEE80211_CIPHER_NONE;
407 wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
408 if (set80211param(priv, IEEE80211_IOC_UCASTCIPHERS, v)) {
409 printf("Unable to set pairwise key ciphers to 0x%x\n", v);
410 return -1;
413 wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
414 __func__, params->wpa_key_mgmt);
415 if (set80211param(priv, IEEE80211_IOC_KEYMGTALGS,
416 params->wpa_key_mgmt)) {
417 printf("Unable to set key management algorithms to 0x%x\n",
418 params->wpa_key_mgmt);
419 return -1;
422 v = 0;
423 if (params->rsn_preauth)
424 v |= BIT(0);
425 wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
426 __func__, params->rsn_preauth);
427 if (set80211param(priv, IEEE80211_IOC_RSNCAPS, v)) {
428 printf("Unable to set RSN capabilities to 0x%x\n", v);
429 return -1;
431 #endif /* IEEE80211_IOC_APPIE */
433 wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, params->wpa);
434 if (set80211param(priv, IEEE80211_IOC_WPA, params->wpa)) {
435 printf("Unable to set WPA to %u\n", params->wpa);
436 return -1;
438 return 0;
441 static int
442 bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params)
444 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled);
446 if (!params->enabled) {
447 /* XXX restore state */
448 return set80211param(priv, IEEE80211_IOC_AUTHMODE,
449 IEEE80211_AUTH_AUTO);
451 if (!params->wpa && !params->ieee802_1x) {
452 wpa_printf(MSG_ERROR, "%s: No 802.1X or WPA enabled",
453 __func__);
454 return -1;
456 if (params->wpa && bsd_configure_wpa(priv, params) != 0) {
457 wpa_printf(MSG_ERROR, "%s: Failed to configure WPA state",
458 __func__);
459 return -1;
461 if (set80211param(priv, IEEE80211_IOC_AUTHMODE,
462 (params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
463 wpa_printf(MSG_ERROR, "%s: Failed to enable WPA/802.1X",
464 __func__);
465 return -1;
467 return bsd_ctrl_iface(priv, 1);
470 static int
471 bsd_set_sta_authorized(void *priv, const u8 *addr,
472 int total_flags, int flags_or, int flags_and)
474 int authorized = -1;
476 /* For now, only support setting Authorized flag */
477 if (flags_or & WPA_STA_AUTHORIZED)
478 authorized = 1;
479 if (!(flags_and & WPA_STA_AUTHORIZED))
480 authorized = 0;
482 if (authorized < 0)
483 return 0;
485 return bsd_send_mlme_param(priv, authorized ?
486 IEEE80211_MLME_AUTHORIZE :
487 IEEE80211_MLME_UNAUTHORIZE, 0, addr);
490 static void
491 bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN])
493 struct ieee80211req_wpaie ie;
494 int ielen = 0;
495 u8 *iebuf = NULL;
498 * Fetch and validate any negotiated WPA/RSN parameters.
500 memset(&ie, 0, sizeof(ie));
501 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
502 if (get80211var(priv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) {
503 printf("Failed to get WPA/RSN information element.\n");
504 goto no_ie;
506 iebuf = ie.wpa_ie;
507 ielen = ie.wpa_ie[1];
508 if (ielen == 0)
509 iebuf = NULL;
510 else
511 ielen += 2;
513 no_ie:
514 drv_event_assoc(ctx, addr, iebuf, ielen);
517 static int
518 bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
519 int encrypt, const u8 *own_addr)
521 struct bsd_driver_data *drv = priv;
523 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", data, data_len);
525 return l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, data,
526 data_len);
529 static int
530 bsd_set_freq(void *priv, u16 channel)
532 struct bsd_driver_data *drv = priv;
533 #ifdef SIOCS80211CHANNEL
534 struct ieee80211chanreq creq;
535 #endif /* SIOCS80211CHANNEL */
536 u32 mode;
538 if (channel < 14)
539 mode = IFM_IEEE80211_11G;
540 else if (channel == 14)
541 mode = IFM_IEEE80211_11B;
542 else
543 mode = IFM_IEEE80211_11A;
544 if (bsd_set_mediaopt(drv, IFM_MMASK, mode) < 0) {
545 wpa_printf(MSG_ERROR, "%s: failed to set modulation mode",
546 __func__);
547 return -1;
550 #ifdef SIOCS80211CHANNEL
551 os_memset(&creq, 0, sizeof(creq));
552 os_strlcpy(creq.i_name, drv->ifname, sizeof(creq.i_name));
553 creq.i_channel = channel;
554 return ioctl(drv->sock, SIOCS80211CHANNEL, &creq);
555 #else /* SIOCS80211CHANNEL */
556 return set80211param(priv, IEEE80211_IOC_CHANNEL, channel);
557 #endif /* SIOCS80211CHANNEL */
560 static int
561 bsd_set_opt_ie(void *priv, const u8 *ie, size_t ie_len)
563 #ifdef IEEE80211_IOC_APPIE
564 wpa_printf(MSG_DEBUG, "%s: set WPA+RSN ie (len %lu)", __func__,
565 (unsigned long)ie_len);
566 return bsd_set80211(priv, IEEE80211_IOC_APPIE, IEEE80211_APPIE_WPA,
567 ie, ie_len);
568 #endif /* IEEE80211_IOC_APPIE */
569 return 0;
573 #ifdef HOSTAPD
576 * Avoid conflicts with hostapd definitions by undefining couple of defines
577 * from net80211 header files.
579 #undef RSN_VERSION
580 #undef WPA_VERSION
581 #undef WPA_OUI_TYPE
583 static int bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
584 int reason_code);
586 static const char *
587 ether_sprintf(const u8 *addr)
589 static char buf[sizeof(MACSTR)];
591 if (addr != NULL)
592 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
593 else
594 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
595 return buf;
598 static int
599 bsd_set_privacy(void *priv, int enabled)
601 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
603 return set80211param(priv, IEEE80211_IOC_PRIVACY, enabled);
606 static int
607 bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
608 u8 *seq)
610 struct ieee80211req_key wk;
612 wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
613 __func__, ether_sprintf(addr), idx);
615 memset(&wk, 0, sizeof(wk));
616 if (addr == NULL)
617 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
618 else
619 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
620 wk.ik_keyix = idx;
622 if (get80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
623 printf("Failed to get encryption.\n");
624 return -1;
627 #ifdef WORDS_BIGENDIAN
630 * wk.ik_keytsc is in host byte order (big endian), need to
631 * swap it to match with the byte order used in WPA.
633 int i;
634 u8 tmp[WPA_KEY_RSC_LEN];
635 memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
636 for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
637 seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
640 #else /* WORDS_BIGENDIAN */
641 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
642 #endif /* WORDS_BIGENDIAN */
643 return 0;
647 static int
648 bsd_flush(void *priv)
650 u8 allsta[IEEE80211_ADDR_LEN];
652 memset(allsta, 0xff, IEEE80211_ADDR_LEN);
653 return bsd_sta_deauth(priv, NULL, allsta, IEEE80211_REASON_AUTH_LEAVE);
657 static int
658 bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
659 const u8 *addr)
661 struct ieee80211req_sta_stats stats;
663 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
664 if (get80211var(priv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats))
665 > 0) {
666 /* XXX? do packets counts include non-data frames? */
667 data->rx_packets = stats.is_stats.ns_rx_data;
668 data->rx_bytes = stats.is_stats.ns_rx_bytes;
669 data->tx_packets = stats.is_stats.ns_tx_data;
670 data->tx_bytes = stats.is_stats.ns_tx_bytes;
672 return 0;
675 static int
676 bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, int reason_code)
678 return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code,
679 addr);
682 static int
683 bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
684 int reason_code)
686 return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code,
687 addr);
690 static void
691 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
693 struct bsd_driver_data *drv = ctx;
694 char buf[2048];
695 struct if_announcemsghdr *ifan;
696 struct rt_msghdr *rtm;
697 struct ieee80211_michael_event *mic;
698 struct ieee80211_join_event *join;
699 struct ieee80211_leave_event *leave;
700 int n;
701 union wpa_event_data data;
703 n = read(sock, buf, sizeof(buf));
704 if (n < 0) {
705 if (errno != EINTR && errno != EAGAIN)
706 perror("read(PF_ROUTE)");
707 return;
710 rtm = (struct rt_msghdr *) buf;
711 if (rtm->rtm_version != RTM_VERSION) {
712 wpa_printf(MSG_DEBUG, "Routing message version %d not "
713 "understood\n", rtm->rtm_version);
714 return;
716 ifan = (struct if_announcemsghdr *) rtm;
717 switch (rtm->rtm_type) {
718 case RTM_IEEE80211:
719 switch (ifan->ifan_what) {
720 case RTM_IEEE80211_ASSOC:
721 case RTM_IEEE80211_REASSOC:
722 case RTM_IEEE80211_DISASSOC:
723 case RTM_IEEE80211_SCAN:
724 break;
725 case RTM_IEEE80211_LEAVE:
726 leave = (struct ieee80211_leave_event *) &ifan[1];
727 drv_event_disassoc(drv->hapd, leave->iev_addr);
728 break;
729 case RTM_IEEE80211_JOIN:
730 #ifdef RTM_IEEE80211_REJOIN
731 case RTM_IEEE80211_REJOIN:
732 #endif
733 join = (struct ieee80211_join_event *) &ifan[1];
734 bsd_new_sta(drv, drv->hapd, join->iev_addr);
735 break;
736 case RTM_IEEE80211_REPLAY:
737 /* ignore */
738 break;
739 case RTM_IEEE80211_MICHAEL:
740 mic = (struct ieee80211_michael_event *) &ifan[1];
741 wpa_printf(MSG_DEBUG,
742 "Michael MIC failure wireless event: "
743 "keyix=%u src_addr=" MACSTR, mic->iev_keyix,
744 MAC2STR(mic->iev_src));
745 os_memset(&data, 0, sizeof(data));
746 data.michael_mic_failure.unicast = 1;
747 data.michael_mic_failure.src = mic->iev_src;
748 wpa_supplicant_event(drv->hapd,
749 EVENT_MICHAEL_MIC_FAILURE, &data);
750 break;
752 break;
756 static void
757 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
759 struct bsd_driver_data *drv = ctx;
760 drv_event_eapol_rx(drv->hapd, src_addr, buf, len);
763 static int
764 hostapd_bsd_set_freq(void *priv, struct hostapd_freq_params *freq)
766 return bsd_set_freq(priv, freq->channel);
769 static void *
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));
775 if (drv == NULL) {
776 printf("Could not allocate memory for bsd driver data\n");
777 goto bad;
780 drv->hapd = hapd;
781 drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
782 if (drv->sock < 0) {
783 perror("socket[PF_INET,SOCK_DGRAM]");
784 goto bad;
786 os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname));
788 drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL,
789 handle_read, drv, 0);
790 if (drv->sock_xmit == NULL)
791 goto bad;
792 if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
793 goto bad;
795 /* mark down during setup */
796 if (bsd_ctrl_iface(drv, 0) < 0)
797 goto bad;
799 drv->route = socket(PF_ROUTE, SOCK_RAW, 0);
800 if (drv->route < 0) {
801 perror("socket(PF_ROUTE,SOCK_RAW)");
802 goto bad;
804 eloop_register_read_sock(drv->route, bsd_wireless_event_receive, drv,
805 NULL);
807 if (bsd_set_mediaopt(drv, IFM_OMASK, IFM_IEEE80211_HOSTAP) < 0) {
808 wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
809 __func__);
810 goto bad;
813 return drv;
814 bad:
815 if (drv->sock_xmit != NULL)
816 l2_packet_deinit(drv->sock_xmit);
817 if (drv->sock >= 0)
818 close(drv->sock);
819 if (drv != NULL)
820 os_free(drv);
821 return NULL;
825 static void
826 bsd_deinit(void *priv)
828 struct bsd_driver_data *drv = priv;
830 if (drv->route >= 0) {
831 eloop_unregister_read_sock(drv->route);
832 close(drv->route);
834 bsd_ctrl_iface(drv, 0);
835 if (drv->sock >= 0)
836 close(drv->sock);
837 if (drv->sock_xmit != NULL)
838 l2_packet_deinit(drv->sock_xmit);
839 os_free(drv);
842 #else /* HOSTAPD */
844 static int
845 get80211param(struct bsd_driver_data *drv, int op)
847 struct ieee80211req ireq;
849 if (bsd_get80211(drv, &ireq, op, NULL, 0) < 0)
850 return -1;
851 return ireq.i_val;
854 static int
855 wpa_driver_bsd_get_bssid(void *priv, u8 *bssid)
857 struct bsd_driver_data *drv = priv;
858 #ifdef SIOCG80211BSSID
859 struct ieee80211_bssid bs;
861 os_strlcpy(bs.i_name, drv->ifname, sizeof(bs.i_name));
862 if (ioctl(drv->sock, SIOCG80211BSSID, &bs) < 0)
863 return -1;
864 os_memcpy(bssid, bs.i_bssid, sizeof(bs.i_bssid));
865 return 0;
866 #else
867 return get80211var(drv, IEEE80211_IOC_BSSID,
868 bssid, IEEE80211_ADDR_LEN) < 0 ? -1 : 0;
869 #endif
872 static int
873 wpa_driver_bsd_get_ssid(void *priv, u8 *ssid)
875 struct bsd_driver_data *drv = priv;
876 return bsd_get_ssid(drv, ssid, 0);
879 static int
880 wpa_driver_bsd_set_wpa_ie(struct bsd_driver_data *drv, const u8 *wpa_ie,
881 size_t wpa_ie_len)
883 #ifdef IEEE80211_IOC_APPIE
884 return bsd_set_opt_ie(drv, wpa_ie, wpa_ie_len);
885 #else /* IEEE80211_IOC_APPIE */
886 return set80211var(drv, IEEE80211_IOC_OPTIE, wpa_ie, wpa_ie_len);
887 #endif /* IEEE80211_IOC_APPIE */
890 static int
891 wpa_driver_bsd_set_wpa_internal(void *priv, int wpa, int privacy)
893 int ret = 0;
895 wpa_printf(MSG_DEBUG, "%s: wpa=%d privacy=%d",
896 __FUNCTION__, wpa, privacy);
898 if (!wpa && wpa_driver_bsd_set_wpa_ie(priv, NULL, 0) < 0)
899 ret = -1;
900 if (set80211param(priv, IEEE80211_IOC_PRIVACY, privacy) < 0)
901 ret = -1;
902 if (set80211param(priv, IEEE80211_IOC_WPA, wpa) < 0)
903 ret = -1;
905 return ret;
908 static int
909 wpa_driver_bsd_set_wpa(void *priv, int enabled)
911 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
913 return wpa_driver_bsd_set_wpa_internal(priv, enabled ? 3 : 0, enabled);
916 static int
917 wpa_driver_bsd_set_countermeasures(void *priv, int enabled)
919 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
920 return set80211param(priv, IEEE80211_IOC_COUNTERMEASURES, enabled);
924 static int
925 wpa_driver_bsd_set_drop_unencrypted(void *priv, int enabled)
927 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
928 return set80211param(priv, IEEE80211_IOC_DROPUNENCRYPTED, enabled);
931 static int
932 wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, int reason_code)
934 return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code,
935 addr);
938 static int
939 wpa_driver_bsd_disassociate(void *priv, const u8 *addr, int reason_code)
941 return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code,
942 addr);
945 static int
946 wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg)
948 int authmode;
950 if ((auth_alg & WPA_AUTH_ALG_OPEN) &&
951 (auth_alg & WPA_AUTH_ALG_SHARED))
952 authmode = IEEE80211_AUTH_AUTO;
953 else if (auth_alg & WPA_AUTH_ALG_SHARED)
954 authmode = IEEE80211_AUTH_SHARED;
955 else
956 authmode = IEEE80211_AUTH_OPEN;
958 return set80211param(priv, IEEE80211_IOC_AUTHMODE, authmode);
961 static void
962 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
964 struct bsd_driver_data *drv = ctx;
966 drv_event_eapol_rx(drv->ctx, src_addr, buf, len);
969 static int
970 wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params)
972 struct bsd_driver_data *drv = priv;
973 struct ieee80211req_mlme mlme;
974 u32 mode;
975 u16 channel;
976 int privacy;
977 int ret = 0;
979 wpa_printf(MSG_DEBUG,
980 "%s: ssid '%.*s' wpa ie len %u pairwise %u group %u key mgmt %u"
981 , __func__
982 , (unsigned int) params->ssid_len, params->ssid
983 , (unsigned int) params->wpa_ie_len
984 , params->pairwise_suite
985 , params->group_suite
986 , params->key_mgmt_suite
989 switch (params->mode) {
990 case IEEE80211_MODE_INFRA:
991 mode = 0 /* STA */;
992 break;
993 case IEEE80211_MODE_IBSS:
994 mode = IFM_IEEE80211_IBSS;
995 break;
996 case IEEE80211_MODE_AP:
997 mode = IFM_IEEE80211_HOSTAP;
998 break;
999 default:
1000 wpa_printf(MSG_ERROR, "%s: unknown operation mode", __func__);
1001 return -1;
1003 if (bsd_set_mediaopt(drv, IFM_OMASK, mode) < 0) {
1004 wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
1005 __func__);
1006 return -1;
1009 if (params->mode == IEEE80211_MODE_AP) {
1010 if (params->freq >= 2412 && params->freq <= 2472)
1011 channel = (params->freq - 2407) / 5;
1012 else if (params->freq == 2484)
1013 channel = 14;
1014 else if ((params->freq >= 5180 && params->freq <= 5240) ||
1015 (params->freq >= 5745 && params->freq <= 5825))
1016 channel = (params->freq - 5000) / 5;
1017 else
1018 channel = 0;
1019 if (bsd_set_freq(drv, channel) < 0)
1020 return -1;
1022 drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL,
1023 handle_read, drv, 0);
1024 if (drv->sock_xmit == NULL)
1025 return -1;
1026 drv->is_ap = 1;
1027 return 0;
1030 if (wpa_driver_bsd_set_drop_unencrypted(drv, params->drop_unencrypted)
1031 < 0)
1032 ret = -1;
1033 if (wpa_driver_bsd_set_auth_alg(drv, params->auth_alg) < 0)
1034 ret = -1;
1035 /* XXX error handling is wrong but unclear what to do... */
1036 if (wpa_driver_bsd_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0)
1037 return -1;
1039 privacy = !(params->pairwise_suite == CIPHER_NONE &&
1040 params->group_suite == CIPHER_NONE &&
1041 params->key_mgmt_suite == KEY_MGMT_NONE &&
1042 params->wpa_ie_len == 0);
1043 wpa_printf(MSG_DEBUG, "%s: set PRIVACY %u", __func__, privacy);
1045 if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0)
1046 return -1;
1048 if (params->wpa_ie_len &&
1049 set80211param(drv, IEEE80211_IOC_WPA,
1050 params->wpa_ie[0] == WLAN_EID_RSN ? 2 : 1) < 0)
1051 return -1;
1053 os_memset(&mlme, 0, sizeof(mlme));
1054 mlme.im_op = IEEE80211_MLME_ASSOC;
1055 if (params->ssid != NULL)
1056 os_memcpy(mlme.im_ssid, params->ssid, params->ssid_len);
1057 mlme.im_ssid_len = params->ssid_len;
1058 if (params->bssid != NULL)
1059 os_memcpy(mlme.im_macaddr, params->bssid, IEEE80211_ADDR_LEN);
1060 if (set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)) < 0)
1061 return -1;
1062 return ret;
1065 static int
1066 wpa_driver_bsd_scan(void *priv, struct wpa_driver_scan_params *params)
1068 struct bsd_driver_data *drv = priv;
1069 #ifdef IEEE80211_IOC_SCAN_MAX_SSID
1070 struct ieee80211_scan_req sr;
1071 int i;
1072 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */
1074 if (bsd_set_mediaopt(drv, IFM_OMASK, 0 /* STA */) < 0) {
1075 wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
1076 __func__);
1077 return -1;
1080 if (set80211param(drv, IEEE80211_IOC_ROAMING,
1081 IEEE80211_ROAMING_MANUAL) < 0) {
1082 wpa_printf(MSG_ERROR, "%s: failed to set "
1083 "wpa_supplicant-based roaming: %s", __func__,
1084 strerror(errno));
1085 return -1;
1088 if (wpa_driver_bsd_set_wpa(drv, 1) < 0) {
1089 wpa_printf(MSG_ERROR, "%s: failed to set wpa: %s", __func__,
1090 strerror(errno));
1091 return -1;
1094 /* NB: interface must be marked UP to do a scan */
1095 if (bsd_ctrl_iface(drv, 1) < 0)
1096 return -1;
1098 #ifdef IEEE80211_IOC_SCAN_MAX_SSID
1099 os_memset(&sr, 0, sizeof(sr));
1100 sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE |
1101 IEEE80211_IOC_SCAN_NOJOIN;
1102 sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER;
1103 if (params->num_ssids > 0) {
1104 sr.sr_nssid = params->num_ssids;
1105 #if 0
1106 /* Boundary check is done by upper layer */
1107 if (sr.sr_nssid > IEEE80211_IOC_SCAN_MAX_SSID)
1108 sr.sr_nssid = IEEE80211_IOC_SCAN_MAX_SSID;
1109 #endif
1111 /* NB: check scan cache first */
1112 sr.sr_flags |= IEEE80211_IOC_SCAN_CHECK;
1114 for (i = 0; i < sr.sr_nssid; i++) {
1115 sr.sr_ssid[i].len = params->ssids[i].ssid_len;
1116 os_memcpy(sr.sr_ssid[i].ssid, params->ssids[i].ssid,
1117 sr.sr_ssid[i].len);
1120 /* NB: net80211 delivers a scan complete event so no need to poll */
1121 return set80211var(drv, IEEE80211_IOC_SCAN_REQ, &sr, sizeof(sr));
1122 #else /* IEEE80211_IOC_SCAN_MAX_SSID */
1123 /* set desired ssid before scan */
1124 if (bsd_set_ssid(drv, params->ssids[0].ssid,
1125 params->ssids[0].ssid_len) < 0)
1126 return -1;
1128 /* NB: net80211 delivers a scan complete event so no need to poll */
1129 return set80211param(drv, IEEE80211_IOC_SCAN_REQ, 0);
1130 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */
1133 static void
1134 wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx)
1136 struct bsd_driver_data *drv = sock_ctx;
1137 char buf[2048];
1138 struct if_announcemsghdr *ifan;
1139 struct if_msghdr *ifm;
1140 struct rt_msghdr *rtm;
1141 union wpa_event_data event;
1142 struct ieee80211_michael_event *mic;
1143 struct ieee80211_leave_event *leave;
1144 struct ieee80211_join_event *join;
1145 int n;
1147 n = read(sock, buf, sizeof(buf));
1148 if (n < 0) {
1149 if (errno != EINTR && errno != EAGAIN)
1150 perror("read(PF_ROUTE)");
1151 return;
1154 rtm = (struct rt_msghdr *) buf;
1155 if (rtm->rtm_version != RTM_VERSION) {
1156 wpa_printf(MSG_DEBUG, "Routing message version %d not "
1157 "understood\n", rtm->rtm_version);
1158 return;
1160 os_memset(&event, 0, sizeof(event));
1161 switch (rtm->rtm_type) {
1162 case RTM_IFANNOUNCE:
1163 ifan = (struct if_announcemsghdr *) rtm;
1164 if (ifan->ifan_index != drv->ifindex)
1165 break;
1166 os_strlcpy(event.interface_status.ifname, drv->ifname,
1167 sizeof(event.interface_status.ifname));
1168 switch (ifan->ifan_what) {
1169 case IFAN_DEPARTURE:
1170 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
1171 default:
1172 return;
1174 wpa_printf(MSG_DEBUG, "RTM_IFANNOUNCE: Interface '%s' %s",
1175 event.interface_status.ifname,
1176 ifan->ifan_what == IFAN_DEPARTURE ?
1177 "removed" : "added");
1178 wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
1179 break;
1180 case RTM_IEEE80211:
1181 ifan = (struct if_announcemsghdr *) rtm;
1182 if (ifan->ifan_index != drv->ifindex)
1183 break;
1184 switch (ifan->ifan_what) {
1185 case RTM_IEEE80211_ASSOC:
1186 case RTM_IEEE80211_REASSOC:
1187 if (drv->is_ap)
1188 break;
1189 wpa_supplicant_event(ctx, EVENT_ASSOC, NULL);
1190 break;
1191 case RTM_IEEE80211_DISASSOC:
1192 if (drv->is_ap)
1193 break;
1194 wpa_supplicant_event(ctx, EVENT_DISASSOC, NULL);
1195 break;
1196 case RTM_IEEE80211_SCAN:
1197 if (drv->is_ap)
1198 break;
1199 wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL);
1200 break;
1201 case RTM_IEEE80211_LEAVE:
1202 leave = (struct ieee80211_leave_event *) &ifan[1];
1203 drv_event_disassoc(ctx, leave->iev_addr);
1204 break;
1205 case RTM_IEEE80211_JOIN:
1206 #ifdef RTM_IEEE80211_REJOIN
1207 case RTM_IEEE80211_REJOIN:
1208 #endif
1209 join = (struct ieee80211_join_event *) &ifan[1];
1210 bsd_new_sta(drv, ctx, join->iev_addr);
1211 break;
1212 case RTM_IEEE80211_REPLAY:
1213 /* ignore */
1214 break;
1215 case RTM_IEEE80211_MICHAEL:
1216 mic = (struct ieee80211_michael_event *) &ifan[1];
1217 wpa_printf(MSG_DEBUG,
1218 "Michael MIC failure wireless event: "
1219 "keyix=%u src_addr=" MACSTR, mic->iev_keyix,
1220 MAC2STR(mic->iev_src));
1222 os_memset(&event, 0, sizeof(event));
1223 event.michael_mic_failure.unicast =
1224 !IEEE80211_IS_MULTICAST(mic->iev_dst);
1225 wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE,
1226 &event);
1227 break;
1229 break;
1230 case RTM_IFINFO:
1231 ifm = (struct if_msghdr *) rtm;
1232 if (ifm->ifm_index != drv->ifindex)
1233 break;
1234 if ((rtm->rtm_flags & RTF_UP) == 0) {
1235 os_strlcpy(event.interface_status.ifname, drv->ifname,
1236 sizeof(event.interface_status.ifname));
1237 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
1238 wpa_printf(MSG_DEBUG, "RTM_IFINFO: Interface '%s' DOWN",
1239 event.interface_status.ifname);
1240 wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
1242 break;
1246 static void
1247 wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res,
1248 struct ieee80211req_scan_result *sr)
1250 struct wpa_scan_res *result, **tmp;
1251 size_t extra_len;
1252 u8 *pos;
1254 extra_len = 2 + sr->isr_ssid_len;
1255 extra_len += 2 + sr->isr_nrates;
1256 extra_len += 3; /* ERP IE */
1257 extra_len += sr->isr_ie_len;
1259 result = os_zalloc(sizeof(*result) + extra_len);
1260 if (result == NULL)
1261 return;
1262 os_memcpy(result->bssid, sr->isr_bssid, ETH_ALEN);
1263 result->freq = sr->isr_freq;
1264 result->beacon_int = sr->isr_intval;
1265 result->caps = sr->isr_capinfo;
1266 result->qual = sr->isr_rssi;
1267 result->noise = sr->isr_noise;
1269 pos = (u8 *)(result + 1);
1271 *pos++ = WLAN_EID_SSID;
1272 *pos++ = sr->isr_ssid_len;
1273 os_memcpy(pos, sr + 1, sr->isr_ssid_len);
1274 pos += sr->isr_ssid_len;
1277 * Deal all rates as supported rate.
1278 * Because net80211 doesn't report extended supported rate or not.
1280 *pos++ = WLAN_EID_SUPP_RATES;
1281 *pos++ = sr->isr_nrates;
1282 os_memcpy(pos, sr->isr_rates, sr->isr_nrates);
1283 pos += sr->isr_nrates;
1285 *pos++ = WLAN_EID_ERP_INFO;
1286 *pos++ = 1;
1287 *pos++ = sr->isr_erp;
1289 os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len, sr->isr_ie_len);
1290 pos += sr->isr_ie_len;
1292 result->ie_len = pos - (u8 *)(result + 1);
1294 tmp = os_realloc(res->res,
1295 (res->num + 1) * sizeof(struct wpa_scan_res *));
1296 if (tmp == NULL) {
1297 os_free(result);
1298 return;
1300 tmp[res->num++] = result;
1301 res->res = tmp;
1304 struct wpa_scan_results *
1305 wpa_driver_bsd_get_scan_results2(void *priv)
1307 struct ieee80211req_scan_result *sr;
1308 struct wpa_scan_results *res;
1309 int len, rest;
1310 uint8_t buf[24*1024], *pos;
1312 len = get80211var(priv, IEEE80211_IOC_SCAN_RESULTS, buf, 24*1024);
1313 if (len < 0)
1314 return NULL;
1316 res = os_zalloc(sizeof(*res));
1317 if (res == NULL)
1318 return NULL;
1320 pos = buf;
1321 rest = len;
1322 while (rest >= sizeof(struct ieee80211req_scan_result)) {
1323 sr = (struct ieee80211req_scan_result *)pos;
1324 wpa_driver_bsd_add_scan_entry(res, sr);
1325 pos += sr->isr_len;
1326 rest -= sr->isr_len;
1329 wpa_printf(MSG_DEBUG, "Received %d bytes of scan results (%lu BSSes)",
1330 len, (unsigned long)res->num);
1332 return res;
1335 static int wpa_driver_bsd_capa(struct bsd_driver_data *drv)
1337 #ifdef IEEE80211_IOC_DEVCAPS
1338 /* kernel definitions copied from net80211/ieee80211_var.h */
1339 #define IEEE80211_CIPHER_WEP 0
1340 #define IEEE80211_CIPHER_TKIP 1
1341 #define IEEE80211_CIPHER_AES_CCM 3
1342 #define IEEE80211_CRYPTO_WEP (1<<IEEE80211_CIPHER_WEP)
1343 #define IEEE80211_CRYPTO_TKIP (1<<IEEE80211_CIPHER_TKIP)
1344 #define IEEE80211_CRYPTO_AES_CCM (1<<IEEE80211_CIPHER_AES_CCM)
1345 #define IEEE80211_C_HOSTAP 0x00000400 /* CAPABILITY: HOSTAP avail */
1346 #define IEEE80211_C_WPA1 0x00800000 /* CAPABILITY: WPA1 avail */
1347 #define IEEE80211_C_WPA2 0x01000000 /* CAPABILITY: WPA2 avail */
1348 struct ieee80211_devcaps_req devcaps;
1350 if (get80211var(drv, IEEE80211_IOC_DEVCAPS, &devcaps,
1351 sizeof(devcaps)) < 0) {
1352 wpa_printf(MSG_ERROR, "failed to IEEE80211_IOC_DEVCAPS: %s",
1353 strerror(errno));
1354 return -1;
1357 wpa_printf(MSG_DEBUG, "%s: drivercaps=0x%08x,cryptocaps=0x%08x",
1358 __func__, devcaps.dc_drivercaps, devcaps.dc_cryptocaps);
1360 if (devcaps.dc_drivercaps & IEEE80211_C_WPA1)
1361 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1362 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1363 if (devcaps.dc_drivercaps & IEEE80211_C_WPA2)
1364 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1365 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1367 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_WEP)
1368 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1369 WPA_DRIVER_CAPA_ENC_WEP104;
1370 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_TKIP)
1371 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1372 if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_AES_CCM)
1373 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1375 if (devcaps.dc_drivercaps & IEEE80211_C_HOSTAP)
1376 drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
1377 #undef IEEE80211_CIPHER_WEP
1378 #undef IEEE80211_CIPHER_TKIP
1379 #undef IEEE80211_CIPHER_AES_CCM
1380 #undef IEEE80211_CRYPTO_WEP
1381 #undef IEEE80211_CRYPTO_TKIP
1382 #undef IEEE80211_CRYPTO_AES_CCM
1383 #undef IEEE80211_C_HOSTAP
1384 #undef IEEE80211_C_WPA1
1385 #undef IEEE80211_C_WPA2
1386 #else /* IEEE80211_IOC_DEVCAPS */
1387 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
1388 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1389 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1390 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1391 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1392 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
1393 WPA_DRIVER_CAPA_ENC_WEP104 |
1394 WPA_DRIVER_CAPA_ENC_TKIP |
1395 WPA_DRIVER_CAPA_ENC_CCMP;
1396 drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
1397 #endif /* IEEE80211_IOC_DEVCAPS */
1398 #ifdef IEEE80211_IOC_SCAN_MAX_SSID
1399 drv->capa.max_scan_ssids = IEEE80211_IOC_SCAN_MAX_SSID;
1400 #else /* IEEE80211_IOC_SCAN_MAX_SSID */
1401 drv->capa.max_scan_ssids = 1;
1402 #endif /* IEEE80211_IOC_SCAN_MAX_SSID */
1403 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
1404 WPA_DRIVER_AUTH_SHARED |
1405 WPA_DRIVER_AUTH_LEAP;
1406 return 0;
1409 static void *
1410 wpa_driver_bsd_init(void *ctx, const char *ifname)
1412 #define GETPARAM(drv, param, v) \
1413 (((v) = get80211param(drv, param)) != -1)
1414 struct bsd_driver_data *drv;
1416 drv = os_zalloc(sizeof(*drv));
1417 if (drv == NULL)
1418 return NULL;
1420 * NB: We require the interface name be mappable to an index.
1421 * This implies we do not support having wpa_supplicant
1422 * wait for an interface to appear. This seems ok; that
1423 * doesn't belong here; it's really the job of devd.
1425 drv->ifindex = if_nametoindex(ifname);
1426 if (drv->ifindex == 0) {
1427 wpa_printf(MSG_DEBUG, "%s: interface %s does not exist",
1428 __func__, ifname);
1429 goto fail1;
1431 drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
1432 if (drv->sock < 0)
1433 goto fail1;
1434 drv->route = socket(PF_ROUTE, SOCK_RAW, 0);
1435 if (drv->route < 0)
1436 goto fail;
1437 eloop_register_read_sock(drv->route,
1438 wpa_driver_bsd_event_receive, ctx, drv);
1440 drv->ctx = ctx;
1441 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
1443 /* Down interface during setup. */
1444 if (bsd_ctrl_iface(drv, 0) < 0)
1445 goto fail;
1447 if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) {
1448 wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s",
1449 __func__, strerror(errno));
1450 goto fail;
1452 if (!GETPARAM(drv, IEEE80211_IOC_PRIVACY, drv->prev_privacy)) {
1453 wpa_printf(MSG_DEBUG, "%s: failed to get privacy state: %s",
1454 __func__, strerror(errno));
1455 goto fail;
1457 if (!GETPARAM(drv, IEEE80211_IOC_WPA, drv->prev_wpa)) {
1458 wpa_printf(MSG_DEBUG, "%s: failed to get wpa state: %s",
1459 __func__, strerror(errno));
1460 goto fail;
1463 if (wpa_driver_bsd_capa(drv))
1464 goto fail;
1466 return drv;
1467 fail:
1468 close(drv->sock);
1469 fail1:
1470 os_free(drv);
1471 return NULL;
1472 #undef GETPARAM
1475 static void
1476 wpa_driver_bsd_deinit(void *priv)
1478 struct bsd_driver_data *drv = priv;
1480 wpa_driver_bsd_set_wpa(drv, 0);
1481 eloop_unregister_read_sock(drv->route);
1483 /* NB: mark interface down */
1484 bsd_ctrl_iface(drv, 0);
1486 wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa, drv->prev_privacy);
1487 if (set80211param(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming) < 0)
1488 wpa_printf(MSG_DEBUG, "%s: failed to restore roaming state",
1489 __func__);
1491 if (drv->sock_xmit != NULL)
1492 l2_packet_deinit(drv->sock_xmit);
1493 (void) close(drv->route); /* ioctl socket */
1494 (void) close(drv->sock); /* event socket */
1495 os_free(drv);
1498 static int
1499 wpa_driver_bsd_get_capa(void *priv, struct wpa_driver_capa *capa)
1501 struct bsd_driver_data *drv = priv;
1503 os_memcpy(capa, &drv->capa, sizeof(*capa));
1504 return 0;
1506 #endif /* HOSTAPD */
1509 const struct wpa_driver_ops wpa_driver_bsd_ops = {
1510 .name = "bsd",
1511 .desc = "BSD 802.11 support",
1512 #ifdef HOSTAPD
1513 .hapd_init = bsd_init,
1514 .hapd_deinit = bsd_deinit,
1515 .set_privacy = bsd_set_privacy,
1516 .get_seqnum = bsd_get_seqnum,
1517 .flush = bsd_flush,
1518 .read_sta_data = bsd_read_sta_driver_data,
1519 .sta_disassoc = bsd_sta_disassoc,
1520 .sta_deauth = bsd_sta_deauth,
1521 .set_freq = hostapd_bsd_set_freq,
1522 #else /* HOSTAPD */
1523 .init = wpa_driver_bsd_init,
1524 .deinit = wpa_driver_bsd_deinit,
1525 .get_bssid = wpa_driver_bsd_get_bssid,
1526 .get_ssid = wpa_driver_bsd_get_ssid,
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,
1533 .get_capa = wpa_driver_bsd_get_capa,
1534 #endif /* HOSTAPD */
1535 .set_key = bsd_set_key,
1536 .set_ieee8021x = bsd_set_ieee8021x,
1537 .hapd_set_ssid = bsd_set_ssid,
1538 .hapd_get_ssid = bsd_get_ssid,
1539 .hapd_send_eapol = bsd_send_eapol,
1540 .sta_set_flags = bsd_set_sta_authorized,
1541 .set_generic_elem = bsd_set_opt_ie,